--- id: 5a24c314108439a4d4036143 title: Extract State Logic to Redux challengeType: 6 isRequired: false videoUrl: '' localeTitle: 将状态逻辑提取到Redux --- ## Description
现在您已完成React组件,您需要将其在其state本地执行的逻辑移动到Redux中。这是将简单的React应用程序连接到Redux的第一步。您的应用程序的唯一功能是将用户的新消息添加到无序列表中。该示例很简单,以演示React和Redux如何协同工作。
## Instructions
首先,定义一个动作类型'ADD'并将其设置为const ADD 。接下来,定义一个动作创建器addMessage() ,它创建添加消息的动作。您需要将message给此操作创建者,并在返回的action包含该消息。然后创建一个名为messageReducer()的reducer来处理消息的状态。初始状态应该等于空数组。此reducer应向状态中保存的消息数组添加消息,或返回当前状态。最后,创建Redux存储并将其传递给reducer。
## Tests
```yml tests: - text: const ADD应该存在并保持一个等于字符串ADD的值 testString: 'assert(ADD === "ADD", "The const ADD should exist and hold a value equal to the string ADD");' - text: 动作创建者addMessage应返回type等于ADD的对象,并且消息等于传入的消息。 testString: 'assert((function() { const addAction = addMessage("__TEST__MESSAGE__"); return addAction.type === ADD && addAction.message === "__TEST__MESSAGE__"; })(), "The action creator addMessage should return an object with type equal to ADD and message equal to the message that is passed in.");' - text: messageReducer应该是一个函数。 testString: 'assert(typeof messageReducer === "function", "messageReducer should be a function.");' - text: 存储应该存在并且初始状态设置为空数组。 testString: 'assert((function() { const initialState = store.getState(); return typeof store === "object" && initialState.length === 0; })(), "The store should exist and have an initial state set to an empty array.");' - text: 对商店调度addMessage应该addMessage向状态中保存的消息数组添加新消息。 testString: 'assert((function() { const initialState = store.getState(); const isFrozen = DeepFreeze(initialState); store.dispatch(addMessage("__A__TEST__MESSAGE")); const addState = store.getState(); return (isFrozen && addState[0] === "__A__TEST__MESSAGE"); })(), "Dispatching addMessage against the store should immutably add a new message to the array of messages held in state.");' - text: 如果使用任何其他操作调用, messageReducer应返回当前状态。 testString: 'assert((function() { const addState = store.getState(); store.dispatch({type: "FAKE_ACTION"}); const testState = store.getState(); return (addState === testState); })(), "The messageReducer should return the current state if called with any other actions.");' ```
## Challenge Seed
```jsx // define ADD, addMessage(), messageReducer(), and store here: ```
## Solution
```js // solution required ```