this is my reducer code
const authReducer = (
state = {
isAuthenticated: !AuthService.isTokenExpired(),
isFetching: false,
profile: AuthService.getProfile(),
error: null
},
action
) => {
switch (action.type) {
case types.LOGOUT_SUCCESS:
return Object.assign({}, state, {
isAuthenticated: false,
isFetching: false,
profile: {},
error: null
})
default:
return state;
}
combinereducer.js
export default combineReducers({
apiDataReducer,
auth: authReducer
});
Main.js
import rootReducer from './reducers/combineReducers';
import reduxImmutableStateInvariant from 'redux-immutable-state-invariant';
imp
ort thunk from 'redux-thunk';
const middleware = process.env.NODE_ENV !== 'production' ? [reduxImmutableStateInvariant(), thunk] : [thunk];
export let store = createStore(rootReducer, applyMiddleware(...middleware));
render(
<Provider store={store}>
<Router>
<App />
</Router>
</Provider>,
document.getElementById('root')
);
Header.js
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom';
import * as authActions from '../../actions/basicActions';
import HeaderView from './HeaderView';
//import authReducer from './reducers/authReducer';
const mapStateToProps = ({ auth }) => ({
auth
});
const mapDispatchToProps = dispatch => ({
loginRequest: () => dispatch(authActions.loginRequest()),
logoutSuccess: () => dispatch(authActions.logoutSuccess())
});
export default withRouter(
connect(mapStateToProps, mapDispatchToProps)(HeaderView)
);
HeaderView.js
handleLogoutClick = () => {
this.props.logoutSuccess();
this.props.history.push({ pathname: '/' });
};
render() {
const { auth } = this.props;
return (
<div>
{auth.isAuthenticated ? (
<div>
<div>
<table>
<tbody>
<label>
<aonClick={this.handleLogoutClick}> Log Out </a>
</label>
</td>
</tr>
</tbody>
</table>
</div>
<nav className="navbar navbar-default">
<ul className="nav navbar-nav">
<li key="1" id="1" onClick={this.handleClick}><Link to={'/page2'}>page2</Link></li>
</ul>
</nav>
</div>
) : (
<label className="Note">
logged in{' '} <a onClick={this.handleLoginClick}> Log In </a>
{' '} to continue.
</label>
)}
</div>
);
}
and this is my container component where state change in reducer auth is not reflecting SomeContainer.js
const mapStateToProps = ({ auth }) => ({
auth
});
const mapDispatchToProps = dispatch => ({
});
export default connect(mapStateToProps, mapDispatchToProps)(SomeContainer);
When logout is clicked in HeaderView.js LOGOUTSUCCESS action gets called and reducer 'auth' changes value of 'isAuthenticated' in object but that change doesnt call 'mapStateToProps' function of connected Container, and value of reducer 'auth' is same i.e. true even after logout.