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

How to know when state is rehydrated? #82

Open
JimLynchCodes opened this issue Oct 31, 2017 · 1 comment
Open

How to know when state is rehydrated? #82

JimLynchCodes opened this issue Oct 31, 2017 · 1 comment

Comments

@JimLynchCodes
Copy link

Hi, I've looked at other similar saving-redux-state-to-local-storage libraries, an they sometimes offer a callback function that is called when the redux state has finished being rehydrated from local storage, but it seems like there is no callback function with this library. Can I safely assume that in my components which implement the mapStateToProps method I can access the locally stored state with this.props.x in my componentWillMount function?

Thanks, Jim.

@gregorskii
Copy link

gregorskii commented Dec 7, 2017

As far as I am aware this library does not have a HOC that lets the components know the LS is loaded. What you can do is use the action types:

import { actionTypes } from 'redux-localstorage';

To set your own field to indicate this:

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case actionTypes.INIT:
      if (action.payload && action.payload.user) {
        const user = action.payload.user;
        // take only what is desired from LS
        return { ...state, ...{
          name: user.name,
          email: user.email,
          localStorageLoaded: true
        }};
      }
      return { ...state };

This way you know in a component localStorageLoaded is set.

This is using the v1 breaking changes build.

However... this does require that you do not persist this field to local storage, otherwise, the loaded prop will exist in the local storage upon hydration. Which technically is fine... as it is hydrated 🤷‍♂️

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