| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | title: Use Provider to Connect Redux to React | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | ## Use Provider to Connect Redux to React
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-30 01:38:07 +11:00
										 |  |  | ### Hint 1
 | 
					
						
							| 
									
										
										
										
											2019-05-09 06:27:06 -07:00
										 |  |  | Render a React Component! | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 06:27:06 -07:00
										 |  |  | ### Hint 2
 | 
					
						
							| 
									
										
										
										
											2019-03-30 01:38:07 +11:00
										 |  |  | You do not need to wrap the `Provider` in any `<div>` tags. | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-30 01:38:07 +11:00
										 |  |  | ### Solution
 | 
					
						
							| 
									
										
										
										
											2019-05-09 06:27:06 -07:00
										 |  |  | <details> | 
					
						
							|  |  |  |   <summary>Spoiler!</summary> | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   ```jsx | 
					
						
							|  |  |  |   // Redux Code: | 
					
						
							|  |  |  |   const ADD = 'ADD'; | 
					
						
							| 
									
										
										
										
											2019-03-30 01:38:07 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 06:27:06 -07:00
										 |  |  |   const addMessage = (message) => { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       type: ADD, | 
					
						
							|  |  |  |       message | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2019-03-30 01:38:07 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 06:27:06 -07:00
										 |  |  |   const messageReducer = (state = [], action) => { | 
					
						
							|  |  |  |     switch (action.type) { | 
					
						
							|  |  |  |       case ADD: | 
					
						
							|  |  |  |         return [ | 
					
						
							|  |  |  |           ...state, | 
					
						
							|  |  |  |           action.message | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |       default: | 
					
						
							|  |  |  |         return state; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2019-03-30 01:38:07 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 06:27:06 -07:00
										 |  |  |   const store = Redux.createStore(messageReducer); | 
					
						
							| 
									
										
										
										
											2019-03-30 01:38:07 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 06:27:06 -07:00
										 |  |  |   // React Code: | 
					
						
							| 
									
										
										
										
											2019-03-30 01:38:07 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 06:27:06 -07:00
										 |  |  |   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 | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2019-03-30 01:38:07 +11:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-09 06:27:06 -07:00
										 |  |  |     submitMessage() { | 
					
						
							|  |  |  |       const currentMessage = this.state.input; | 
					
						
							|  |  |  |       this.setState({ | 
					
						
							|  |  |  |         input: '', | 
					
						
							|  |  |  |         messages: this.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> | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2019-03-30 01:38:07 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 06:27:06 -07:00
										 |  |  |   const Provider = ReactRedux.Provider; | 
					
						
							| 
									
										
										
										
											2019-03-30 01:38:07 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 06:27:06 -07:00
										 |  |  |   class AppWrapper extends React.Component { | 
					
						
							|  |  |  |     // Below is the code required to pass the test | 
					
						
							|  |  |  |     render() { | 
					
						
							|  |  |  |       return ( | 
					
						
							|  |  |  |         <Provider store={store}> | 
					
						
							|  |  |  |           <DisplayMessages /> | 
					
						
							|  |  |  |         </Provider> | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // Above is the code required to pass the test | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   ``` | 
					
						
							|  |  |  | </details> |