2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 5a24c314108439a4d4036146
|
2021-03-05 08:43:24 -07:00
|
|
|
|
title: 映射 Dispatch 到 Props
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 6
|
2020-09-07 16:11:48 +08:00
|
|
|
|
forumTopicId: 301432
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: map-dispatch-to-props
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
`mapDispatchToProps()` 函数可为 React 组件提供特定的创建 action 的函数,以便组件可 dispatch actions,从而更改 Redux store 中的数据。 该函数的结构跟上一挑战中的`mapStateToProps()`函数相似, 它返回一个对象,把 dispatch actions 映射到属性名上,该属性名成为`props`。 然而,每个属性都返回一个用 action creator 及与 action 相关的所有数据调用 `dispatch` 的函数,而不是返回 `state` 的一部分。 可以访问 `dispatch`,因为在定义函数时,我们以参数形式把它传入 `mapDispatchToProps()` 了,这跟 `state` 传入 `mapStateToProps()` 是一样的。 在幕后,React Redux 用 Redux 的 `store.dispatch()` 来管理这些含 `mapDispatchToProps()` 的dispatches, 这跟它使用 `store.subscribe()` 来订阅映射到 `state` 的组件的方式类似。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
例如,创建 action 的函数 `loginUser()` 把 `username` 作为 action payload, `mapDispatchToProps()` 返回给创建 action 的函数的对象如下:
|
2020-09-07 16:11:48 +08:00
|
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
|
{
|
|
|
|
|
submitLoginUser: function(username) {
|
|
|
|
|
dispatch(loginUser(username));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
编辑器上提供的是创建 action 的函数 `addMessage()`。 写出接收 `dispatch` 为参数的函数 `mapDispatchToProps()`,返回一个 dispatch 函数对象, 其属性为 `submitNewMessage`。该函数在 dispatch `addMessage()` 时为新消息提供一个参数。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
`addMessage` 应返回含 `type` 和 `message` 两个键的对象。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
const addMessageTest = addMessage();
|
|
|
|
|
return (
|
|
|
|
|
addMessageTest.hasOwnProperty('type') &&
|
|
|
|
|
addMessageTest.hasOwnProperty('message')
|
|
|
|
|
);
|
|
|
|
|
})()
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
`mapDispatchToProps` 应为函数。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(typeof mapDispatchToProps === 'function');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
`mapDispatchToProps` 应返回一个对象。
|
2020-09-07 16:11:48 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(typeof mapDispatchToProps() === 'object');
|
|
|
|
|
```
|
2020-09-07 16:11:48 +08:00
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
从 `mapDispatchToProps` 通过 `submitNewMessage` 分发 `addMessage`,应向 dispatch 函数返回一条消息。
|
2020-09-07 16:11:48 +08:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
let testAction;
|
|
|
|
|
const dispatch = (fn) => {
|
|
|
|
|
testAction = fn;
|
|
|
|
|
};
|
|
|
|
|
let dispatchFn = mapDispatchToProps(dispatch);
|
|
|
|
|
dispatchFn.submitNewMessage('__TEST__MESSAGE__');
|
|
|
|
|
return (
|
|
|
|
|
testAction.type === 'ADD' && testAction.message === '__TEST__MESSAGE__'
|
|
|
|
|
);
|
|
|
|
|
})()
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
|
const addMessage = (message) => {
|
|
|
|
|
return {
|
|
|
|
|
type: 'ADD',
|
|
|
|
|
message: message
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Change code below this line
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```jsx
|
|
|
|
|
const addMessage = (message) => {
|
|
|
|
|
return {
|
|
|
|
|
type: 'ADD',
|
|
|
|
|
message: message
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Change code below this line
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => {
|
|
|
|
|
return {
|
|
|
|
|
submitNewMessage: function(message) {
|
|
|
|
|
dispatch(addMessage(message));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|