State is not updating after user logged in.
Opened this issue · 1 comments
When user Logged in the state of User reducer should update with token and email. But in redux devtool action showing user object having email and token property data from server but state is empty. i am following your tutorial so below are codes the progress i have made up to.
code for api.js
import axios from 'axios'
export default {
user:{
login:(credential) => axios.post('/api/auth',{credential}).then(res => res.data.user)
}
}
code for auth.js
import {USER_LOGGED_IN} from '../types'
import api from '../api'
export const userLoggedIn = user => ({
type: USER_LOGGED_IN,
user
});
export const login = (credential) =>(dispatch) => api.user.login(credential).then(user => dispatch(userLoggedIn(user)))
code for reducer user.js
import { USER_LOGGED_IN } from "../types";
const initailState = {
email:'',
token:''
}
export default function user(state = initailState, action = {}) {
switch (action.type) {
case USER_LOGGED_IN:
return action.user;
default:
return state;
}
}
rootreducer code
import { combineReducers } from "redux";
import user from "./reducers/user";
// import books from "./reducers/books";
export default combineReducers({
user:()=>({})
});
Go here: 01e35d1 and scroll down to the rootReducer.js change about 2/3 the way down the page. There you will see:
This is why the state was not updating. I had this issue too and I think it didn't make it into the video. The change is there in the commits though. Hope this helps you.