301 lines
		
	
	
		
			7.8 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
		
		
			
		
	
	
			301 lines
		
	
	
		
			7.8 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
|   | --- | ||
|  | id: 5a24c314108439a4d4036148 | ||
|  | title: Redux をメッセージアプリに接続する | ||
|  | challengeType: 6 | ||
|  | forumTopicId: 301427 | ||
|  | dashedName: connect-redux-to-the-messages-app | ||
|  | --- | ||
|  | 
 | ||
|  | # --description--
 | ||
|  | 
 | ||
|  | `connect` を使用して React を Redux に接続する方法を理解したところで、メッセージを処理する React コンポーネントに応用してみましょう。 | ||
|  | 
 | ||
|  | 前回のレッスンで、Redux に接続したコンポーネントは `Presentational` という名前でしたが、これは勝手に付けたものではありません。 この用語は*全般的に*、Redux に直接接続していない React コンポーネントのことを指します。 これらのコンポーネントは、単に UI の表現部分 (プレゼンテーション) を担い、自身が受け取る props の関数としてその機能を実行します。 これに対して、コンテナーコンポーネントは Redux に接続します。 これらは通常、ストアにアクションをディスパッチする役割を担い、多くの場合、ストアの state を props として子コンポーネントに渡します。 | ||
|  | 
 | ||
|  | # --instructions--
 | ||
|  | 
 | ||
|  | コードエディターには、ここまでこのセクションで記述したすべてのコードがあります。 一つだけ、React コンポーネントの名前が `Presentational` に変更されています。 `Container` という定数に保持する新しいコンポーネントを作成してください。このコンポーネントは、`connect` を使用して `Presentational` コンポーネントを Redux に接続します。 次に、`AppWrapper` の中で React Redux の `Provider` コンポーネントをレンダーしてください。 `Provider` に Redux の `store` を prop として渡し、`Container` を子としてレンダーしてください。 すべての設定が完了すると、再びメッセージアプリがページにレンダーされます。 | ||
|  | 
 | ||
|  | # --hints--
 | ||
|  | 
 | ||
|  | `AppWrapper` をページにレンダーします。 | ||
|  | 
 | ||
|  | ```js | ||
|  | assert( | ||
|  |   (function () { | ||
|  |     const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); | ||
|  |     return mockedComponent.find('AppWrapper').length === 1; | ||
|  |   })() | ||
|  | ); | ||
|  | ``` | ||
|  | 
 | ||
|  | `Presentational` コンポーネントをページにレンダーします。 | ||
|  | 
 | ||
|  | ```js | ||
|  | assert( | ||
|  |   (function () { | ||
|  |     const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); | ||
|  |     return mockedComponent.find('Presentational').length === 1; | ||
|  |   })() | ||
|  | ); | ||
|  | ``` | ||
|  | 
 | ||
|  | `Presentational` コンポーネントで、`h2`、`input`、`button`、`ul` の各要素をレンダーします。 | ||
|  | 
 | ||
|  | ```js | ||
|  | 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 | ||
|  |     ); | ||
|  |   })() | ||
|  | ); | ||
|  | ``` | ||
|  | 
 | ||
|  | `Presentational` コンポーネントで、Redux ストアから `messages` を prop として受け取ります。 | ||
|  | 
 | ||
|  | ```js | ||
|  | assert( | ||
|  |   (function () { | ||
|  |     const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); | ||
|  |     const PresentationalComponent = mockedComponent.find('Presentational'); | ||
|  |     const props = PresentationalComponent.props(); | ||
|  |     return Array.isArray(props.messages); | ||
|  |   })() | ||
|  | ); | ||
|  | ``` | ||
|  | 
 | ||
|  | `Presentational` コンポーネントで、`submitMessage` アクションクリエイターを prop として受け取ります。 | ||
|  | 
 | ||
|  | ```js | ||
|  | assert( | ||
|  |   (function () { | ||
|  |     const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); | ||
|  |     const PresentationalComponent = mockedComponent.find('Presentational'); | ||
|  |     const props = PresentationalComponent.props(); | ||
|  |     return typeof props.submitNewMessage === 'function'; | ||
|  |   })() | ||
|  | ); | ||
|  | ``` | ||
|  | 
 | ||
|  | # --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); | ||
|  |   } | ||
|  | }; | ||
|  | ``` | ||
|  | 
 | ||
|  | # --solutions--
 | ||
|  | 
 | ||
|  | ```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> | ||
|  |     ); | ||
|  |   } | ||
|  | }; | ||
|  | ``` |