Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State is not updating after user logged in. #19

Open
Tayyab-Shinwari opened this issue Nov 9, 2019 · 1 comment
Open

State is not updating after user logged in. #19

Tayyab-Shinwari opened this issue Nov 9, 2019 · 1 comment

Comments

@Tayyab-Shinwari
Copy link

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:()=>({})

});

@kicksent
Copy link

Go here: 01e35d1 and scroll down to the rootReducer.js change about 2/3 the way down the page. There you will see:

image

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants