this.props.authState
stays the same although I\'m dispatching an action in my componentDidMount
function:
componentDidMount() {
You're currently dispatching directly inside the componentDidMount which isn't mapped into:
connect(mapStateToProps, mapDispatchToProps)(Home);
This should do the job:
componentDidMount() {
if (localStorage.getItem('token')) {
this.props.onUpdateAuthState('AUTHENTICATED');
}
}
const mapDispatchToProps = (dispatch) => {
return {
onUpdateAuthState: function(authState) {
dispatch(updateAuthState(authState));
}
}
};
Now, this will get the authState:
const mapStateToProps = (state) => {
return {
authState: state.authState
}
};
If you’re mutating the state then component will not be re rendered. You need to return {...state, authState}
from your reducer.
And you verify your updated state in
componentWillReceiveProps(nextProps)
{
}
I hope this would solve the issue.