--- id: 5a24c314108439a4d403614f title: Dispatch an Action Event challengeType: 6 isRequired: false videoUrl: '' localeTitle: 派遣行动事件 --- ## Description
dispatch方法是您用于将操作分派给Redux存储的方法。调用store.dispatch()并传递从操作创建者返回的值会将操作发送回商店。回想一下,动作创建者返回一个具有type属性的对象,该属性指定已发生的动作。然后,该方法将操作对象调度到Redux存储。根据之前的挑战示例,以下行是等效的,并且都调度LOGIN类型的操作:
store.dispatch(actionCreator());
store.dispatch({type:'LOGIN'});
## Instructions
代码编辑器中的Redux存储具有初始化状态,该状态是包含当前设置为falselogin属性的对象。还有一个名为loginAction()的动作创建器,它返回LOGIN类型的动作。通过调用dispatch方法将LOGIN操作发送到Redux存储,并传入loginAction()创建的操作。
## Tests
```yml tests: - text: 调用函数loginAction应该返回一个对象,其type属性设置为字符串LOGIN 。 testString: 'assert(loginAction().type === "LOGIN", "Calling the function loginAction should return an object with type property set to the string LOGIN.");' - text: 应使用属性login设置为false的对象初始化存储。 testString: 'assert(store.getState().login === false, "The store should be initialized with an object with property login set to false.");' - text: store.dispatch()方法应该用于调度LOGIN类型的操作。 testString: 'getUserInput => assert((function() { let noWhiteSpace = getUserInput("index").replace(/\s/g,""); return noWhiteSpace.includes("store.dispatch(loginAction())") || noWhiteSpace.includes("store.dispatch({type: \"LOGIN\"})") === true })(), "The store.dispatch() method should be used to dispatch an action of type LOGIN.");' ```
## Challenge Seed
```jsx const store = Redux.createStore( (state = {login: false}) => state ); const loginAction = () => { return { type: 'LOGIN' } }; // Dispatch the action here: ```
## Solution
```js // solution required ```