createStore()
方法。为了让我们将多个reducer组合在一起,Redux提供了combineReducers()
方法。此方法接受一个对象作为参数,您可以在其中定义将键与特定reducer函数关联的属性。 Redux将使用您为密钥指定的名称作为相关状态的名称。通常,当每个应用程序状态以某种方式不同或唯一时,为每个应用程序状态创建一个减速器是一个好习惯。例如,在具有用户身份验证的笔记记录应用程序中,一个reducer可以处理身份验证,而另一个reducer处理用户正在提交的文本和备注。对于这样的应用程序,我们可能会像这样编写combineReducers()
方法: const rootReducer = Redux.combineReducers({现在,关键
auth:authenticationReducer,
笔记:notesReducer
});
notes
将包含与我们的注释相关联的所有状态,并由notesReducer
处理。这就是如何组合多个reducers来管理更复杂的应用程序状态。在此示例中,Redux存储中保存的状态将是包含auth
和notes
属性的单个对象。 authReducer()
counterReducer()
和authReducer()
函数以及Redux存储。使用Redux.combineReducers()
方法完成rootReducer()
函数的Redux.combineReducers()
。指定counterReducer
一键叫count
和authReducer
一个叫关键auth
。 counterReducer
应该递增和递减state
。
testString: 'assert((function() { const initalState = store.getState().count; store.dispatch({type: INCREMENT}); store.dispatch({type: INCREMENT}); const firstState = store.getState().count; store.dispatch({type: DECREMENT}); const secondState = store.getState().count; return firstState === initalState + 2 && secondState === firstState - 1 })(), "The counterReducer
should increment and decrement the state
.");'
- text: authReducer
应切换在true
和false
之间进行authenticated
的state
。
testString: 'assert((function() { store.dispatch({type: LOGIN}); const loggedIn = store.getState().auth.authenticated; store.dispatch({type: LOGOUT}); const loggedOut = store.getState().auth.authenticated; return loggedIn === true && loggedOut === false })(), "The authReducer
should toggle the state
of authenticated
between true
and false
.");'
- text: 存储state
应该有两个键: count
,它包含一个数字, auth
,它包含一个对象。 auth
对象应具有authenticated
属性,该属性包含布尔值。
testString: 'assert((function() { const state = store.getState(); return typeof state.auth === "object" && typeof state.auth.authenticated === "boolean" && typeof state.count === "number" })(), "The store state
should have two keys: count
, which holds a number, and auth
, which holds an object. The auth
object should have a property of authenticated
, which holds a boolean.");'
- text: 该rootReducer
应该是结合了功能counterReducer
和authReducer
。
testString: 'getUserInput => assert((function() { const noWhiteSpace = getUserInput("index").replace(/\s/g,""); return typeof rootReducer === "function" && noWhiteSpace.includes("Redux.combineReducers") })(), "The rootReducer
should be a function that combines the counterReducer
and the authReducer
.");'
```