Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redux: Passing a preloadedstate with combined reducer and store enhancer

I created a redux store with the reducer and enhancer functions.

    const store = createStore(rootReducer, compose(
      applyMiddleware(thunk),
      DevTools.instrument()
    ))

rootReducer uses combineReducers to create a single root reducer. I would like to initialize the window's location value within the store so I tried something like this:

 const initialWindowLocation = window.location.hostname
 const store = createStore(rootReducer, initialWindowLocation, compose(
      applyMiddleware(thunk),
      DevTools.instrument()
 ))

I connected the store to the redux forms and I'm able to grab all the reducer values via mapStateToProps but I'm stuck trying to access a simple string value(i.e.initialWindowLocation) from the redux createStore. Is there some redux tricks that I'm missing?

Also if I try that I'm getting an unexpected key error:

 Unexpected key found in previous state received by the reducer.

Any thoughts on this? Thanks in advance.

like image 578
InquisitiveGirl Avatar asked Aug 26 '17 02:08

InquisitiveGirl


People also ask

How do I pass multiple reducers to store?

It turns out that Redux lets us combine multiple reducers into one that can be passed into createStore by using a helper function named combineReducers . The way we combine reducers is simple, we create one file per reducer in the reducers directory. We also create a file called index. js inside the reducers directory.

What is preloadedState Redux?

[preloadedState] (any): The initial state. You may optionally specify it to hydrate the state from the server in universal apps, or to restore a previously serialized user session. If you produced reducer with combineReducers, this must be a plain object with the same shape as the keys passed to it.

What does createStore do in Redux?

createStore(reducer, [preloadedState], [enhancer]) Creates a Redux store that holds the complete state tree of your app. There should only be a single store in your app.


1 Answers

As per the redux docs here,

createStore(reducer, [preloadedState], [enhancer])

[preloadedState] (any): The initial state. You may optionally specify it to hydrate the state from the server in universal apps, or to restore a previously serialized user session. If you produced reducer with combineReducers, this must be a plain object with the same shape as the keys passed to it. Otherwise, you are free to pass anything that your reducer can understand.

Its better to create one more reducers.

const window = ( state = window.location.hostname,action )=>state;

and add it to the combineReducers list.

EDIT :

I'm wondering why passing in the plain object as the second argument to the createStore doesn't work

Assume your rootReducer look like this one.

const window = (state = { location: "test" }, action) => state;
const user = (state = "", action) => state;
const rootReducer = combineReducers({ window, user });

When you say "passing in the plain object as the second argument to the createStore", Its means,preloadedstate should be matching the reducer default state.

preloadedstate = { user: [], window: { location: "windowone" } }`

const store = createStore(rootReducer, { user: [], window: { location: "windowone" } }, applyMiddleware(...middleWare));

otherwise,you will get an error Unexpected key found in previous state received by the reducer. as no such key defined in reducers.

like image 195
RIYAJ KHAN Avatar answered Nov 03 '22 18:11

RIYAJ KHAN