how to set initial state in redux

Saad picture Saad · Jun 15, 2016 · Viewed 70.8k times · Source

I'm trying to figure out how to set an initial state for a store in redux. I'm using https://github.com/reactjs/redux/blob/master/examples/todos-with-undo/reducers/index.js as an example. I tried to modify the code such that the todos had a value initialized.

const todoApp = combineReducers({
  todos,
  visibilityFilter
}, {
  todos: [{id:123, text:'hello', completed: false}]
})

following the doc: http://redux.js.org/docs/api/createStore.html

but it's not working, and I'm not quite sure why.

Answer

ctrlplusb picture ctrlplusb · Jun 15, 2016

It needs to be the second argument to createStore:

const rootReducer = combineReducers({
  todos: todos,
  visibilityFilter: visibilityFilter
});

const initialState = { 
  todos: [{id:123, text:'hello', completed: false}] 
};

const store = createStore(
  rootReducer, 
  initialState
);