Tried to follow a simple guide posted on this forum, but it’s returned undefined for the state variables ‘greeting’ and ‘name’. This is index.js in a create react app. What did I miss that is causing the state not to pass properly?
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import * as Redux from "redux";
import * as ReactRedux from "react-redux";
import ReduxDevtools from "redux-devtools";
const initial_state = {
greeting : "hello",
name : "joe"
};
const reducer = (prior_state = initial_state, action) => {
switch (action.type) {
case 'CHANGE_NAME':
return {
...prior_state,
name : action.payload.name
};
default:
return prior_state;
}
};
const store = Redux.createStore(reducer, initial_state, window.devToolsExtension && window.devToolsExtension());
// action creator / function hybrid
class TestComponent extends Component {
handle_click = () => {
const change_name_joseph = {
type : "CHANGE_NAME",
payload : { name : "Joseph" }
};
this.props.dispatch(change_name_joseph)
};
render() {
return (
<div>
<h1>{`${this.props.greeting} to my friend ${this.props.name}`}</h1>
<button onClick={() => this.handle_click}> Click here</button>
</div>
)
}
}
ReactRedux.connect((initial_state) => {
return {
greeting : initial_state.greeting,
name : initial_state.name
}
})(TestComponent); // The arg for the returned function
// This is what RR will bind to.
// It also injects the dispatch function
// last step (or first?)
ReactDOM.render(
<ReactRedux.Provider store={store}>
<TestComponent />
</ReactRedux.Provider>, document.getElementById('root'));
serviceWorker.unregister();