Files
freeCodeCamp/curriculum/challenges/chinese/03-front-end-libraries/redux/dispatch-an-action-event.md
Oliver Eyton-Williams ee1e8abd87 feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

86 lines
1.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
id: 5a24c314108439a4d403614f
title: 分发 Action Event
challengeType: 6
forumTopicId: 301442
dashedName: dispatch-an-action-event
---
# --description--
`dispatch`方法用于将 action 分派给 Redux store调用`store.dispatch()`将从 action creator 返回的值发送回 store。
action creator 返回一个具有 type 属性的对象,该属性指定已发生的 action然后该方法将 action 对象 dispatch 到 Redux store根据之前的挑战示例以下内容是等效的并且都 dispatch 类型为`LOGIN`的 action
```js
store.dispatch(actionCreator());
store.dispatch({ type: 'LOGIN' });
```
# --instructions--
代码编辑器中的 Redux store 具有初始化状态对象{login:'false'},还有一个名为`loginAction()`的 action creator它返回类型为`LOGIN`的 action然后通过调用`dispatch`方法将`LOGIN`的 action dispatch 给 Redux store并传递`loginAction()`创建的 action。
# --hints--
调用函数`loginAction`应该返回一个对象{type:"LOGIN"}。
```js
assert(loginAction().type === 'LOGIN');
```
store 应该初始化一个对象 {login:false}。
```js
assert(store.getState().login === false);
```
`store.dispatch()`方法应该被用于 dispatch 一个类型为`LOGIN`的 action。
```js
(getUserInput) =>
assert(
(function () {
let noWhiteSpace = getUserInput('index').replace(/\s/g, '');
return (
noWhiteSpace.includes('store.dispatch(loginAction())') ||
noWhiteSpace.includes("store.dispatch({type: 'LOGIN'})") === true
);
})()
);
```
# --seed--
## --seed-contents--
```js
const store = Redux.createStore(
(state = {login: false}) => state
);
const loginAction = () => {
return {
type: 'LOGIN'
}
};
// Dispatch the action here:
```
# --solutions--
```js
const store = Redux.createStore(
(state = {login: false}) => state
);
const loginAction = () => {
return {
type: 'LOGIN'
}
};
store.dispatch(loginAction());
```