This is App.js
class App extends Component {
render() {
return (
<Provider store={store}>
<Router>
<Route exact path ="/" component={AuthPage} />
<Route exact path ="/login" component={AuthPage} />
<Route exact path ="/register" component={AuthPage} />
<Route exact path ="/homepage" component={Homepage} />
<Route exact path ="/notloggedin" component={NotLoggedIn} />
</Router>
</Provider>
);
}
}
export default App;
This is my action
import jwt_decode from 'jwt-decode'
import { SET_CURRENT_USER } from './types';
export const checkUser = user => (dispatch) => {
fetch('/login', {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(user)
})
.then(res => res.json())
.then((json) => {
if (json.status === 200) {
localStorage.setItem("jwt", json.jwt);
const { jwt } = json
const { user_name } = jwt_decode(jwt);
dispatch({
type: SET_CURRENT_USER,
payload: user_name
})
window.location = '/homepage'
} else {
alert(json.message)
}
});
};
Here is my reducer
import { SET_CURRENT_USER } from '../actions/types'
const initialState = {
user: ''
};
export default function (state = initialState, action) {
switch (action.type) {
case SET_CURRENT_USER:
return {
user: action.payload,
}
default:
return state
}
}
I can set an alert inside of the reduction to show action.payload and it appears alert
But Redux Dev Tools show no update to state redux dev tools
Why isn't state being updated?
json.status === 200
is never going to be true because your json response will be a string not a number. Does it work if you either change it to compare against a string or just remove the condition entirely? – jmargolisvtreturn {...state, user: action.payload}
and see what you get. – Steve K