Search code examples
react-nativereduxreact-reduxreact-native-firebase

Compare values before and after updating redux state


I am listening to a Firebase node.....then when a change occurs in FBS Im comparing the snapshot versus what's currently in Redux. Crazy thing is that Redux state already reflects the updated value in FBS (from snapshot) even before I've dispatched

const listenToFbsMyUserNode = (dispatch) => {
  database()
    .ref('profiles/users/')
    .on('value', snapshot => {
      let state = store.getState(); //at this point Redux somehow already knows about the updated value before Ive dispatched!!!
      //now can dispatch
      dispatch(_refreshMyProfile(snapshot.val()));
    });
};  

Solution

  • That means that you are probably copying Firebase objects into your store. Since Firebase does not work immutably, Firebase does modify these objects - essentially also modifying your store with a dispatch, since your store now contains references to exactly these objects. Something like that should of course not happen - convert your objects into flat data before dispatching them instead of dispatching the Firebase objects directly.