简体   繁体   中英

state not updating in reducer in react native

I'm using react-redux for mantaining state of my app. This is my action

case 'set_user_name':
      user.updateProfile({displayName: action.payload});
      database().ref(`Users/${state.user.id}/name`).set(action.payload);
      return {
        ...state,
        name: action.payload,
      };

In this action I am receiving name perfectly and in my database it is also updating fine but it does not update my name in state. here is my state

if (user != null) {
  Initial_state = {
    defaultPic: defaultPic,
    user: {
      id: user ? user.uid : '',
      name: user.displayName ? user.displayName : 'set name',
      photoURL: user.photoURL ? user.photoURL : defaultPic,
      phoneNumber: user ? user.phoneNumber : '',
      fcmToken: '',
    },
    allUsers: '',
  };
}

your Initial_state holds a user , wich is where you place the name attribute.

I am supposing that action.payload just return the user's name

update your case and check if it works.

case 'set_user_name':
      user.updateProfile({displayName: action.payload});
      database().ref(`Users/${state.user.id}/name`).set(action.payload);
      return {
        ...state,
        user: { ...state.user, name: action.payload },
      };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM