Files
freeCodeCamp/curriculum/challenges/chinese/03-front-end-libraries/redux/use-a-switch-statement-to-handle-multiple-actions.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

153 lines
3.7 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: 5a24c314108439a4d4036151
title: 使用 Switch 语句处理多个 Actions
challengeType: 6
forumTopicId: 301449
dashedName: use-a-switch-statement-to-handle-multiple-actions
---
# --description--
你可以定义 Redux store 如何处理多种 action 类型。比如你正在 Redux store 中进行用户身份验证,如果你希望用户在登录和注销时具有状态的响应,你可以使用具有`authenticated`属性的单个的 state 对象。你还需要使用 action creators 创建与用户登录和用户注销相对应的 action以及 action 对象本身。
# --instructions--
代码编辑器为你创建了 store、actions、action creators。通过编写`reducer`函数来处理多个身份验证操作。可以在`reducer`通过使用 JavaScript 的`switch`来响应不同的 action 事件。这是编写 Redux reducer 时的标准模式switch 语句选择`action.type`中的一个值并返回相应的身份验证状态。
**注意:** 此时,不要担心 state 的不变性,因为在这个示例中它很小而且很简单。所以对于每个操作你都可以返回一个新对象,比如`{authenticated:true}`。另外,不要忘记在 switch 语句中写一个`default` case返回当前的`state`。这是很重要的,因为一旦你的程序有多个 reducer当一个 action 被 dispatch 时它们都会运行,即使 action 与该 reducer 无关。在这种情况下,你要确保返回当前的`state`
# --hints--
调用`loginUser`函数应该返回一个 {type:"LOGIN"} 对象。
```js
assert(loginUser().type === 'LOGIN');
```
调用`logoutUser`函数应该返回一个 {type:"LOGOUT"} 对象。
```js
assert(logoutUser().type === 'LOGOUT');
```
store 应该设置一个初始化对象 {authenticated:false}。
```js
assert(store.getState().authenticated === false);
```
dispatch `loginUser`应该将 store 中的 state 的`authenticated`值更新为`true`
```js
assert(
(function () {
const initialState = store.getState();
store.dispatch(loginUser());
const afterLogin = store.getState();
return (
initialState.authenticated === false && afterLogin.authenticated === true
);
})()
);
```
dispatch `logoutUser`应该将 store 中的 state 的`authenticated`值更新为`false`
```js
assert(
(function () {
store.dispatch(loginUser());
const loggedIn = store.getState();
store.dispatch(logoutUser());
const afterLogout = store.getState();
return (
loggedIn.authenticated === true && afterLogout.authenticated === false
);
})()
);
```
`authReducer`函数应该使用`switch`语句处理多个 action 类型。
```js
(getUserInput) =>
assert(
getUserInput('index').toString().includes('switch') &&
getUserInput('index').toString().includes('case') &&
getUserInput('index').toString().includes('default')
);
```
# --seed--
## --seed-contents--
```js
const defaultState = {
authenticated: false
};
const authReducer = (state = defaultState, action) => {
// Change code below this line
// Change code above this line
};
const store = Redux.createStore(authReducer);
const loginUser = () => {
return {
type: 'LOGIN'
}
};
const logoutUser = () => {
return {
type: 'LOGOUT'
}
};
```
# --solutions--
```js
const defaultState = {
authenticated: false
};
const authReducer = (state = defaultState, action) => {
switch (action.type) {
case 'LOGIN':
return {
authenticated: true
}
case 'LOGOUT':
return {
authenticated: false
}
default:
return state;
}
};
const store = Redux.createStore(authReducer);
const loginUser = () => {
return {
type: 'LOGIN'
}
};
const logoutUser = () => {
return {
type: 'LOGOUT'
}
};
```