type
之外的任何信息。您还可以发送特定数据以及您的操作。事实上,这是非常常见的,因为动作通常源于一些用户交互,并倾向于携带一些数据。 Redux商店经常需要了解这些数据。 notesReducer()
和一个addNoteText()
动作创建器。完成addNoteText()
函数的主体,以便它返回一个action
对象。该对象应包含值为ADD_NOTE
的type
属性,以及设置为传递给action creator的note
数据的text
属性。当您致电操作创建者时,您将传递可以访问该对象的特定注释信息。接下来,完成在notesReducer()
编写switch
语句。您需要添加一个处理addNoteText()
操作的案例。只要存在类型为ADD_NOTE
的操作,就应该触发此情况,并且应该将传入action
的text
属性作为新state
。该操作将在代码底部发送。完成后,运行代码并观察控制台。这就是将特定于操作的数据发送到商店并在更新存储state
时使用它所需的全部内容。 addNoteText
应返回具有键type
和text
的对象。
testString: 'assert((function() { const addNoteFn = addNoteText("__TEST__NOTE"); return addNoteFn.type === ADD_NOTE && addNoteFn.text === "__TEST__NOTE" })(), "The action creator addNoteText
should return an object with keys type
and text
.");'
- text: 使用addNoteText
操作创建程序调度ADD_NOTE
类型的操作应将state
更新为传递给操作创建者的字符串。
testString: 'assert((function() { const initialState = store.getState(); store.dispatch(addNoteText("__TEST__NOTE")); const newState = store.getState(); return initialState !== newState && newState === "__TEST__NOTE" })(), "Dispatching an action of type ADD_NOTE
with the addNoteText
action creator should update the state
to the string passed to the action creator.");'
```