2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 5a24c314108439a4d4036148
|
2021-03-05 08:43:24 -07:00
|
|
|
|
title: 将 Redux 连接到 Messages App
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 6
|
2020-09-07 16:11:48 +08:00
|
|
|
|
forumTopicId: 301427
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: connect-redux-to-the-messages-app
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
知道如何使用 `connect` 连接 React 和 Redux 后,我们可以在 React 组件中应用上面学到的内容。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
在上一课,连接到 Redux 的组件命名为 `Presentational`,这个命名不是任意的, 这样的术语*通常*是指未直接连接到 Redux 的 React 组件, 它们只负责执行接收 props 的函数来实现 UI 的呈现。 相比之下,容器组件用来连接到 Redux 上。 这些组件通常负责把 actions 分派给 store,且经常给子组件传入 store state 属性。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
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
|
|
|
|
到目前为止,我们的编辑器上已包含了整个章节的代码, 唯一不同的是,React 组件被重新命名为 `Presentational`,即展示层组件。 创建一个新组件,保存在名为 `Container` 的常量中。 这个常量用 `connect` 把 `Presentational` 组件和 Redux 连接起来。 然后,在`AppWrapper` 中渲染 React Redux 的 `Provider`组件, 给 `Provider` 传入 Redux `store` 属性并渲染 `Container` 为子组件。 设置完所有内容后,将再次看到消息应用程序渲染到页面上。
|
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-09-18 11:22:53 -07:00
|
|
|
|
`AppWrapper` 应该渲染该到页面上。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
const mockedComponent = Enzyme.mount(React.createElement(AppWrapper));
|
|
|
|
|
return mockedComponent.find('AppWrapper').length === 1;
|
|
|
|
|
})()
|
|
|
|
|
);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
|
`Presentational` 应该渲染到页面上.
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
const mockedComponent = Enzyme.mount(React.createElement(AppWrapper));
|
|
|
|
|
return mockedComponent.find('Presentational').length === 1;
|
|
|
|
|
})()
|
|
|
|
|
);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
`Presentational` 组件应渲染 `h2`、`input`、`button`、`ul` 四个元素。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
const mockedComponent = Enzyme.mount(React.createElement(AppWrapper));
|
|
|
|
|
const PresentationalComponent = mockedComponent.find('Presentational');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
return (
|
2020-12-16 00:37:30 -07:00
|
|
|
|
PresentationalComponent.find('div').length === 1 &&
|
|
|
|
|
PresentationalComponent.find('h2').length === 1 &&
|
|
|
|
|
PresentationalComponent.find('button').length === 1 &&
|
|
|
|
|
PresentationalComponent.find('ul').length === 1
|
2018-10-10 18:03:03 -04:00
|
|
|
|
);
|
2020-12-16 00:37:30 -07:00
|
|
|
|
})()
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-05 08:43:24 -07:00
|
|
|
|
`Presentational` 组件应接收 Redux store 的 `messages` 属性。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
const mockedComponent = Enzyme.mount(React.createElement(AppWrapper));
|
|
|
|
|
const PresentationalComponent = mockedComponent.find('Presentational');
|
|
|
|
|
const props = PresentationalComponent.props();
|
|
|
|
|
return Array.isArray(props.messages);
|
|
|
|
|
})()
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
|
`Presentational` 组件应接收创建 action 的函数的 `submitMessage` 属性。
|
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(
|
|
|
|
|
(function () {
|
|
|
|
|
const mockedComponent = Enzyme.mount(React.createElement(AppWrapper));
|
|
|
|
|
const PresentationalComponent = mockedComponent.find('Presentational');
|
|
|
|
|
const props = PresentationalComponent.props();
|
|
|
|
|
return typeof props.submitNewMessage === 'function';
|
|
|
|
|
})()
|
|
|
|
|
);
|
|
|
|
|
```
|
2020-09-07 16:11:48 +08:00
|
|
|
|
|
2021-01-13 00:22:19 +08:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
|
ReactDOM.render(<AppWrapper />, document.getElementById('root'))
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
|
// Redux:
|
|
|
|
|
const ADD = 'ADD';
|
|
|
|
|
|
|
|
|
|
const addMessage = (message) => {
|
|
|
|
|
return {
|
|
|
|
|
type: ADD,
|
|
|
|
|
message: message
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const messageReducer = (state = [], action) => {
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
case ADD:
|
|
|
|
|
return [
|
|
|
|
|
...state,
|
|
|
|
|
action.message
|
|
|
|
|
];
|
|
|
|
|
default:
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const store = Redux.createStore(messageReducer);
|
|
|
|
|
|
|
|
|
|
// React:
|
|
|
|
|
class Presentational extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
input: '',
|
|
|
|
|
messages: []
|
|
|
|
|
}
|
|
|
|
|
this.handleChange = this.handleChange.bind(this);
|
|
|
|
|
this.submitMessage = this.submitMessage.bind(this);
|
|
|
|
|
}
|
|
|
|
|
handleChange(event) {
|
|
|
|
|
this.setState({
|
|
|
|
|
input: event.target.value
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
submitMessage() {
|
|
|
|
|
this.setState((state) => {
|
|
|
|
|
const currentMessage = state.input;
|
|
|
|
|
return {
|
|
|
|
|
input: '',
|
|
|
|
|
messages: state.messages.concat(currentMessage)
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<h2>Type in a new Message:</h2>
|
|
|
|
|
<input
|
|
|
|
|
value={this.state.input}
|
|
|
|
|
onChange={this.handleChange}/><br/>
|
|
|
|
|
<button onClick={this.submitMessage}>Submit</button>
|
|
|
|
|
<ul>
|
|
|
|
|
{this.state.messages.map( (message, idx) => {
|
|
|
|
|
return (
|
|
|
|
|
<li key={idx}>{message}</li>
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// React-Redux:
|
|
|
|
|
const mapStateToProps = (state) => {
|
|
|
|
|
return { messages: state }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => {
|
|
|
|
|
return {
|
|
|
|
|
submitNewMessage: (newMessage) => {
|
|
|
|
|
dispatch(addMessage(newMessage))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const Provider = ReactRedux.Provider;
|
|
|
|
|
const connect = ReactRedux.connect;
|
|
|
|
|
|
|
|
|
|
// Define the Container component here:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AppWrapper extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
}
|
|
|
|
|
render() {
|
|
|
|
|
// Complete the return statement:
|
|
|
|
|
return (null);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
2020-09-07 16:11:48 +08:00
|
|
|
|
|
2021-01-13 00:22:19 +08:00
|
|
|
|
```jsx
|
|
|
|
|
// Redux:
|
|
|
|
|
const ADD = 'ADD';
|
|
|
|
|
|
|
|
|
|
const addMessage = (message) => {
|
|
|
|
|
return {
|
|
|
|
|
type: ADD,
|
|
|
|
|
message: message
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const messageReducer = (state = [], action) => {
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
case ADD:
|
|
|
|
|
return [
|
|
|
|
|
...state,
|
|
|
|
|
action.message
|
|
|
|
|
];
|
|
|
|
|
default:
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const store = Redux.createStore(messageReducer);
|
|
|
|
|
|
|
|
|
|
// React:
|
|
|
|
|
class Presentational extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
input: '',
|
|
|
|
|
messages: []
|
|
|
|
|
}
|
|
|
|
|
this.handleChange = this.handleChange.bind(this);
|
|
|
|
|
this.submitMessage = this.submitMessage.bind(this);
|
|
|
|
|
}
|
|
|
|
|
handleChange(event) {
|
|
|
|
|
this.setState({
|
|
|
|
|
input: event.target.value
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
submitMessage() {
|
|
|
|
|
this.setState((state) => {
|
|
|
|
|
const currentMessage = state.input;
|
|
|
|
|
return {
|
|
|
|
|
input: '',
|
|
|
|
|
messages: state.messages.concat(currentMessage)
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<h2>Type in a new Message:</h2>
|
|
|
|
|
<input
|
|
|
|
|
value={this.state.input}
|
|
|
|
|
onChange={this.handleChange}/><br/>
|
|
|
|
|
<button onClick={this.submitMessage}>Submit</button>
|
|
|
|
|
<ul>
|
|
|
|
|
{this.state.messages.map( (message, idx) => {
|
|
|
|
|
return (
|
|
|
|
|
<li key={idx}>{message}</li>
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// React-Redux:
|
|
|
|
|
const mapStateToProps = (state) => {
|
|
|
|
|
return { messages: state }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => {
|
|
|
|
|
return {
|
|
|
|
|
submitNewMessage: (newMessage) => {
|
|
|
|
|
dispatch(addMessage(newMessage))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const Provider = ReactRedux.Provider;
|
|
|
|
|
const connect = ReactRedux.connect;
|
|
|
|
|
|
|
|
|
|
const Container = connect(mapStateToProps, mapDispatchToProps)(Presentational);
|
|
|
|
|
|
|
|
|
|
class AppWrapper extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
}
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<Provider store={store}>
|
|
|
|
|
<Container/>
|
|
|
|
|
</Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|