how to set initial state in redux

前端 未结 4 1098
梦如初夏
梦如初夏 2020-12-03 04:07

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.j

4条回答
  •  醉话见心
    2020-12-03 04:47

    You can set the initial state in the reducer(s).

    const initialTodos = [{id:123, text:'hello', completed: false}]
    
    // this is the ES2015 syntax for setting a default value for state in the function parameters
    function todoReducer(state = initialTodos, action) {
      switch(action.type) {
        ... 
      }
      return state
    }
    
    
    const todoApp = combineReducers({
      // todos now defaults to the array of todos that you wanted and will be updated when you pass a new set of todos to the todoReducer
      todos: todoReducer,
      visibilityFilter
    })
    

提交回复
热议问题