KELiON / redux-async-initial-state

Redux middleware that helps you load redux initial state asynchronously
MIT License
114 stars 12 forks source link

Error when using asyncInitialState.outerReducer on first load #18

Closed SMJ93 closed 6 years ago

SMJ93 commented 6 years ago

I keep getting the following Redux error when using the asyncInitialState.outerReducer function in React Native on first load.

console.error: "The previous state received by the reducer has unexpected type of "Function". Expected argument to be an object with the following keys: "app", "routing", "asyncInitialState""

If I reload the app it works as expected, loading the state from AsyncStorage.

Here is our configureStore function:

const appReducers = combineReducers({
    app: app.appSlice,
    routing: routerReducer,
    asyncInitialState: asyncInitialState.innerReducer
});

const rootReducer = (state, action) => {
    if (action.type === 'AUTH_SIGN_OUT') {
      state = {
        app: app.signOutStatePartial(state)
      };
    }
    return appReducers(state, action);
  };

// This here seems to be the cause of the issue
const reducer = asyncInitialState.outerReducer(rootReducer);

return createReduxStore(
    reducer,  // setting this to `rootReducer` gets rid of the error
    applyMiddleware(
        asyncInitialState.middleware(loadStore),
        ...
    )
)

If I change reducer in the createReduxStore to rootReducer the app will load, but won't load the localState from AsyncStorage.

There seems to be an issue with the asyncInitialState.outerReducer function.

conor909 commented 6 years ago

this issue is related to #13

You can view the merge diff here

So where ever you reference the loadStore functions currentState argument, you need to trigger it as a function instead of referencing it as the state object.

so currentState should actually be something like getCurrentState()

KELiON commented 6 years ago

Thanks for the issue, @conor909! You're right, we forgot to change readme. Now it is updated, so you can change your code regarding it – now your loadStore function should receive function argument instead of an object.

SMJ93 commented 6 years ago

@conor909, @KELiON great thanks guys!