0

I'm having a little problem at trying to save my firebase user data into the Redux store, I have already tried a lot of waysto save the data in a my store like that:

const initialState = {
  logged: null,
  user: {}
};

Inside my user object I want all data from my firebase. I send the data like that:

firebase.auth().onAuthStateChanged(user => {
  if (user) {
    store.dispatch({
      type: "USER_LOGIN",
      payload: { user: { ...user.providerData[0] } }
    });
  } else {
    store.dispatch({ type: "USER_LOGOUT" });
  }
});

Why when am I trying to acces to the data need I to use this.props.user.user.displayName? With the couple of user, note that if I use only one user i receive undefined (Obviusly).

Diego Cardona
  • 355
  • 1
  • 3
  • 12

1 Answers1

0
firebase.auth().onAuthStateChanged(user => {
  if (user) {
    store.dispatch({
      type: "USER_LOGIN",
      payload: user.providerData[0]
    });
  } else {
    store.dispatch({ type: "USER_LOGOUT" });
  }
});

Does that work? Just modified payload.

CtheGood
  • 999
  • 8
  • 17
  • Yea, that's work but is better send a object, like this. { ...user.providerData[0] }. I set your answer to working. Thanks! – Diego Cardona Jan 07 '19 at 21:38
  • Just out of curiosity, why is it better to send `{ ...user.providerData[0] }`? That is more computationally heavy, when it accomplishes the same thing as `payload: user.providerData[0]`. It also uses more memory, creating a shallow copy. – CtheGood Jan 07 '19 at 21:43
  • Yep? Lol I'dont know, I' be ask later to teacher that's teachs it online. Really I'm a noob and I'm learning seeing the code of others – Diego Cardona Jan 07 '19 at 22:06
  • I've seen it a lot in Redux stuff. I think it's something to do with this https://stackoverflow.com/a/43153020/1512654 – khartnett Jan 07 '19 at 22:51