--- id: 5a24c314108439a4d4036148 title: Connect Redux to the Messages App challengeType: 6 isRequired: false videoUrl: '' localeTitle: 将Redux连接到消息应用程序 --- ## Description
现在您已了解如何使用connect将React连接到Redux,您可以将您学到的知识应用于处理消息的React组件。在上一课中,您连接到Redux的组件名为Presentational ,这不是任意的。该术语通常是指未直接连接到Redux的React组件。他们只负责UI的呈现,并根据他们收到的道具来执行此操作。相比之下,容器组件连接到Redux。这些通常负责将操作分派给商店,并且经常将商店状态作为道具传递给子组件。
## Instructions
到目前为止,代码编辑器包含了您在本节中编写的所有代码。唯一的变化是React组件被重命名为Presentational 。创建一个名为Container的常量中保存的新组件,该常量使用connectPresentational组件连接到Redux。然后,在AppWrapper ,渲染React Redux Provider组件。将Redux store Provider作为道具传递,并将Container作为子项呈现。设置完所有内容后,您将再次看到应用程序呈现给页面的消息。
## Tests
```yml tests: - text: AppWrapper应该呈现给页面。 testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); return mockedComponent.find("AppWrapper").length === 1; })(), "The AppWrapper should render to the page.");' - text: Presentational组件应呈现h2inputbuttonul元素。 testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); return mockedComponent.find("Presentational").length === 1; })(), "The Presentational component should render an h2, input, button, and ul elements.");' - text: Presentational组件应呈现h2inputbuttonul元素。 testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); const PresentationalComponent = mockedComponent.find("Presentational"); return ( PresentationalComponent.find("div").length === 1 && PresentationalComponent.find("h2").length === 1 && PresentationalComponent.find("button").length === 1 && PresentationalComponent.find("ul").length === 1 ); })(), "The Presentational component should render an h2, input, button, and ul elements.");' - text: Presentational组件应该从Redux商店接收messages作为道具。 testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); const PresentationalComponent = mockedComponent.find("Presentational"); const props = PresentationalComponent.props(); return Array.isArray(props.messages); })(), "The Presentational component should receive messages from the Redux store as a prop.");' - text: Presentational组件应该接收submitMessage操作创建者作为prop。 testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); const PresentationalComponent = mockedComponent.find("Presentational"); const props = PresentationalComponent.props(); return typeof props.submitNewMessage === "function"; })(), "The Presentational component should receive the submitMessage action creator as a prop.");' ```
## Challenge Seed
```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() { const currentMessage = this.state.input; this.setState({ input: ", messages: this.state.messages.concat(currentMessage) }); } render() { return (

Type in a new Message:


    {this.state.messages.map( (message, idx) => { return (
  • {message}
  • ) }) }
); } }; // 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); } }; ```
### After Test
```js console.info('after the test'); ```
## Solution
```js // solution required ```