2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 5a24c314108439a4d403614e
|
|
|
|
|
title: Define an Action Creator
|
|
|
|
|
challengeType: 6
|
|
|
|
|
isRequired: false
|
2020-09-07 16:16:17 +08:00
|
|
|
|
forumTopicId: 301441
|
|
|
|
|
localeTitle: 定义一个 Action Creator
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-09-07 16:16:17 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
创建 action 后要将 action 发送到 Redux store,以便它可以更新其状态。在 Redux 中,你可以定义动作创建器来完成此任务,action creator 只是一个返回动作的 JavaScript 函数,换句话说,action creator 创建表示动作事件的对象。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-09-07 16:16:17 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
定义名为<code>actionCreator()</code>的函数,该函数在调用时返回<code>action</code>对象。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
|
|
|
|
- text: 函数<code>actionCreator</code>应该存在。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(typeof actionCreator === 'function');
|
2020-09-07 16:16:17 +08:00
|
|
|
|
- text: 运行<code>actionCreator</code>函数应返回 action 对象。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(typeof action === 'object');
|
2020-09-07 16:16:17 +08:00
|
|
|
|
- text: 返回的 action 应具有值为<code>LOGIN</code>的键值类型。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(action.type === 'LOGIN');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='jsx-seed'>
|
|
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
|
const action = {
|
|
|
|
|
type: 'LOGIN'
|
|
|
|
|
}
|
2020-09-07 16:16:17 +08:00
|
|
|
|
// 在此处定义 action creator
|
|
|
|
|
|
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 action = {
|
|
|
|
|
type: 'LOGIN'
|
|
|
|
|
}
|
|
|
|
|
// 在此处定义 action creator:
|
|
|
|
|
const actionCreator = () => {
|
|
|
|
|
return action;
|
|
|
|
|
};
|
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>
|