--- id: 5a24c314108439a4d4036155 title: Send Action Data to the Store challengeType: 6 isRequired: false videoUrl: '' localeTitle: 将操作数据发送到商店 --- ## Description
到目前为止,您已经学会了如何将操作分派给Redux存储,但到目前为止,这些操作还没有包含除type之外的任何信息。您还可以发送特定数据以及您的操作。事实上,这是非常常见的,因为动作通常源于一些用户交互,并倾向于携带一些数据。 Redux商店经常需要了解这些数据。
## Instructions
在代码编辑器中定义了一个基本的notesReducer()和一个addNoteText()动作创建器。完成addNoteText()函数的主体,以便它返回一个action对象。该对象应包含值为ADD_NOTEtype属性,以及设置为传递给action creator的note数据的text属性。当您致电操作创建者时,您将传递可以访问该对象的特定注释信息。接下来,完成在notesReducer()编写switch语句。您需要添加一个处理addNoteText()操作的案例。只要存在类型为ADD_NOTE的操作,就应该触发此情况,并且应该将传入actiontext属性作为新state 。该操作将在代码底部发送。完成后,运行代码并观察控制台。这就是将特定于操作的数据发送到商店并在更新存储state时使用它所需的全部内容。
## Tests
```yml tests: - text: 动作创建者addNoteText应返回具有键typetext的对象。 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.");' ```
## Challenge Seed
```jsx const ADD_NOTE = 'ADD_NOTE'; const notesReducer = (state = 'Initial State', action) => { switch(action.type) { // change code below this line // change code above this line default: return state; } }; const addNoteText = (note) => { // change code below this line // change code above this line }; const store = Redux.createStore(notesReducer); console.log(store.getState()); store.dispatch(addNoteText('Hello!')); console.log(store.getState()); ```
## Solution
```js // solution required ```