--- id: 5a24c314108439a4d4036144 title: Use Provider to Connect Redux to React challengeType: 6 isRequired: false videoUrl: '' localeTitle: 使用Provider将Redux连接到React --- ## Description
在上一个挑战中,您创建了一个Redux存储来处理messages数组并创建了一个用于添加新消息的操作。下一步是提供对Redux存储的React访问以及分派更新所需的操作。 React Redux提供了react-redux包来帮助完成这些任务。 React Redux提供了一个小API,它有两个关键特性: Providerconnect 。另一个挑战包括connectProvider是React Redux的一个包装组件,它包装了你的React应用程序。然后,此包装器允许您访问整个组件树中的Redux storedispatch功能。 Provider需要两个道具,Redux商店和应用程序的子组件。为App组件定义Provider可能如下所示:
<Provider store = {store}>
<应用/>
</提供商>
## Instructions
代码编辑器现在显示过去几个挑战中的所有Redux和React代码。它包括Redux存储,操作和DisplayMessages组件。唯一的新部分是底部的AppWrapper组件。使用此顶级组件从ReactRedux呈现Provider ,并将Redux存储作为prop传递。然后将DisplayMessages组件渲染为子级。完成后,您应该看到React组件呈现给页面。 注意: React Redux在此处可用作全局变量,因此您可以使用点表示法访问提供程序。编辑器中的代码利用了这一点并将其设置为一个常量Provider供您在AppWrapper渲染方法中使用。
## 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.");' - text: Provider包装器组件应该具有传递给它的store支柱,等于Redux存储。 testString: 'getUserInput => assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); return getUserInput("index").replace(/\s/g,"").includes(""); })(), "The Provider wrapper component should have a prop of store passed to it, equal to the Redux store.");' - text: DisplayMessages应该呈现为AppWrapper 。 testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); return mockedComponent.find("AppWrapper").find("DisplayMessages").length === 1; })(), "DisplayMessages should render as a child of AppWrapper.");' - text: DisplayMessages组件应该呈现h2,input,button和ul元素。 testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); return mockedComponent.find("div").length === 1 && mockedComponent.find("h2").length === 1 && mockedComponent.find("button").length === 1 && mockedComponent.find("ul").length === 1; })(), "The DisplayMessages component should render an h2, input, button, and ul element.");' ```
## Challenge Seed
```jsx // Redux Code: const ADD = 'ADD'; const addMessage = (message) => { return { type: ADD, message } }; const messageReducer = (state = [], action) => { switch (action.type) { case ADD: return [ ...state, action.message ]; default: return state; } }; const store = Redux.createStore(messageReducer); // React Code: class DisplayMessages 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}
  • ) }) }
); } }; const Provider = ReactRedux.Provider; class AppWrapper extends React.Component { // render the Provider here // change code above this line }; ```
### After Test
```js console.info('after the test'); ```
## Solution
```js // solution required ```