I have a react component being rendered client side using react router. I have a redux store that is being successfully populated after a dispatch (thus I believe the action and reducer are working correctly)
The problem comes when the connect mapStateToProps function fires, the state is empty.
I have subscribed to the store and console.log(store.getState()) and I can see the state getting set correctly.
Where am I going wrong?
class MyComponent extends Component {
componentWillMount(){
this.props.fetchData();
}
render(){
console.log(this.props);
return(<div>{this.props}</div>
}
const mapStateToProps = (state) => {
console.log(state); //This is empty e.g. { form : {} }
return {
formJSON: state.form
}
};
const mapDispatchToProps = { fetchData};
export default connect( mapStateToProps, mapDispatchToProps )(MyComponent);
The react router stuff
class ClientApp extends Component {
render() {
return (
<Provider store={store}>
<Router history={history}>
<Switch>
<Route exact path="/" >
<div>Root</div>
</Route>
<Route path='/mycomponent'>
<Container fluid>
<Row>
<Col lg={2}>
<MyComponent/>
</Col>
<Col lg={7}>
<OtherComponent />
</Col>
<Col>
<YetAnotherComponent/>
</Col>
</Row>
</Container>
</Route>
</Switch>
</Router>
</Provider>
);
}
}
export default ClientApp;
return(<pre>{JSON.stringify(this.props,undefined,2)}</pre>
– HMR