2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 5a24c314108439a4d403614f
|
|
|
|
|
title: Dispatch an Action Event
|
|
|
|
|
challengeType: 6
|
|
|
|
|
isRequired: false
|
2020-09-07 16:16:17 +08:00
|
|
|
|
forumTopicId: 301442
|
|
|
|
|
localeTitle: 分发 Action Event
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-09-07 16:16:17 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
<code>dispatch</code>方法用于将 action 分派给 Redux store,调用<code>store.dispatch()</code>将从 action creator 返回的值发送回 store。
|
|
|
|
|
action creator 返回一个具有 type 属性的对象,该属性指定已发生的 action,然后,该方法将 action 对象 dispatch 到 Redux store,根据之前的挑战示例,以下内容是等效的,并且都 dispatch 类型为<code>LOGIN</code>的 action:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
store.dispatch(actionCreator());
|
|
|
|
|
store.dispatch({ type: 'LOGIN' });
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-09-07 16:16:17 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
代码编辑器中的 Redux store 具有初始化状态对象{login:'false'},还有一个名为<code>loginAction()</code>的 action creator,它返回类型为<code>LOGIN</code>的 action,然后通过调用<code>dispatch</code>方法将<code>LOGIN</code>的 action dispatch 给 Redux store,并传递<code>loginAction()</code>创建的 action。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
2020-09-07 16:16:17 +08:00
|
|
|
|
- text: '调用函数<code>loginAction</code>应该返回一个对象{type:"LOGIN"}。'
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(loginAction().type === 'LOGIN');
|
2020-09-07 16:16:17 +08:00
|
|
|
|
- text: 'store 应该初始化一个对象 {login:false}。'
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(store.getState().login === false);
|
2020-09-07 16:16:17 +08:00
|
|
|
|
- text: <code>store.dispatch()</code>方法应该被用于 dispatch 一个类型为<code>LOGIN</code>的 action。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: "getUserInput => assert((function() { let noWhiteSpace = getUserInput('index').replace(/\\s/g,''); return noWhiteSpace.includes('store.dispatch(loginAction())') || noWhiteSpace.includes('store.dispatch({type: \\'LOGIN\\'})') === true })());"
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='jsx-seed'>
|
|
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
|
const store = Redux.createStore(
|
|
|
|
|
(state = {login: false}) => state
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const loginAction = () => {
|
|
|
|
|
return {
|
|
|
|
|
type: 'LOGIN'
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 16:16:17 +08:00
|
|
|
|
// 在这里 dispatch action
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
2020-09-07 16:16:17 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```js
|
2020-09-07 16:16:17 +08:00
|
|
|
|
const store = Redux.createStore(
|
|
|
|
|
(state = {login: false}) => state
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const loginAction = () => {
|
|
|
|
|
return {
|
|
|
|
|
type: 'LOGIN'
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Dispatch the action here:
|
|
|
|
|
store.dispatch(loginAction());
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2020-09-07 16:16:17 +08:00
|
|
|
|
</section>
|