2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 5a24c314108439a4d4036149
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Extract Local State into Redux
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								challengeType: 6
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 301428
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								dashedName: extract-local-state-into-redux
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --description--
  
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You're almost done! Recall that you wrote all the Redux code so that Redux could control the state management of your React messages app. Now that Redux is connected, you need to extract the state management out of the `Presentational`  component and into Redux. Currently, you have Redux connected, but you are handling the state locally within the `Presentational`  component.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --instructions--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In the `Presentational`  component, first, remove the `messages`  property in the local `state` . These messages will be managed by Redux. Next, modify the `submitMessage()`  method so that it dispatches `submitNewMessage()`  from `this.props` , and pass in the current message input from local `state`  as an argument. Because you removed `messages`  from local state, remove the `messages`  property from the call to `this.setState()`  here as well. Finally, modify the `render()`  method so that it maps over the messages received from `props`  rather than `state` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Once these changes are made, the app will continue to function the same, except Redux manages the state. This example also illustrates how a component may have local `state` : your component still tracks user input locally in its own `state` . You can see how Redux provides a useful state management framework on top of React. You achieved the same result using only React's local state at first, and this is usually possible with simple apps. However, as your apps become larger and more complex, so does your state management, and this is the problem Redux solves.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --hints--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `AppWrapper`  should render to the page.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const mockedComponent = Enzyme.mount(React.createElement(AppWrapper));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return mockedComponent.find('AppWrapper').length === 1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `Presentational`  component should render to page.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								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.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```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
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `Presentational`  component should receive `messages`  from the Redux store as a 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);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `Presentational`  component should receive the `submitMessage`  action creator as a 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';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The state of the `Presentational`  component should contain one property, `input` , which is initialized to an empty string.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const mockedComponent = Enzyme.mount(React.createElement(AppWrapper));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const PresentationalState = mockedComponent
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      .find('Presentational')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      .instance().state;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return (
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      typeof PresentationalState.input === 'string' & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      Object.keys(PresentationalState).length === 1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Typing in the `input`  element should update the state of the `Presentational`  component.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const mockedComponent = Enzyme.mount(React.createElement(AppWrapper));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const testValue = '__MOCK__INPUT__';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const waitForIt = (fn) =>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 100));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const causeChange = (c, v) =>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    c.find('input').simulate('change', { target: { value: v } });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  let initialInput = mockedComponent.find('Presentational').find('input');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const changed = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    causeChange(mockedComponent, testValue);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return waitForIt(() => mockedComponent);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const updated = await changed();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const updatedInput = updated.find('Presentational').find('input');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    initialInput.props().value === '' & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      updatedInput.props().value === '__MOCK__INPUT__'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Dispatching the `submitMessage`  on the `Presentational`  component should update Redux store and clear the input in local state.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const mockedComponent = Enzyme.mount(React.createElement(AppWrapper));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const waitForIt = (fn) =>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 100));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  let beforeProps = mockedComponent.find('Presentational').props();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const testValue = '__TEST__EVENT__INPUT__';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const causeChange = (c, v) =>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    c.find('input').simulate('change', { target: { value: v } });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const changed = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    causeChange(mockedComponent, testValue);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return waitForIt(() => mockedComponent);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const clickButton = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    mockedComponent.find('button').simulate('click');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return waitForIt(() => mockedComponent);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const afterChange = await changed();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const afterChangeInput = afterChange.find('input').props().value;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const afterClick = await clickButton();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const afterProps = mockedComponent.find('Presentational').props();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    beforeProps.messages.length === 0 & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      afterChangeInput === testValue & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      afterProps.messages.pop() === testValue & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      afterClick.find('input').props().value === ''
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `Presentational`  component should render the `messages`  from the Redux store.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const mockedComponent = Enzyme.mount(React.createElement(AppWrapper));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const waitForIt = (fn) =>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 100));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  let beforeProps = mockedComponent.find('Presentational').props();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const testValue = '__TEST__EVENT__INPUT__';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const causeChange = (c, v) =>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    c.find('input').simulate('change', { target: { value: v } });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const changed = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    causeChange(mockedComponent, testValue);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return waitForIt(() => mockedComponent);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const clickButton = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    mockedComponent.find('button').simulate('click');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return waitForIt(() => mockedComponent);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const afterChange = await changed();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const afterChangeInput = afterChange.find('input').props().value;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const afterClick = await clickButton();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const afterProps = mockedComponent.find('Presentational').props();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    beforeProps.messages.length === 0 & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      afterChangeInput === testValue & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      afterProps.messages.pop() === testValue & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      afterClick.find('input').props().value === '' & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      afterClick.find('ul').childAt(0).text() === testValue
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --seed--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## --after-user-code--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```jsx
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								ReactDOM.render(< AppWrapper  / > , document.getElementById('root'))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## --seed-contents--
  
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01: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:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const Provider = ReactRedux.Provider;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const connect = ReactRedux.connect;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Change code below this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class Presentational extends React.Component {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  constructor(props) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    super(props);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.state = {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      input: '',
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								      messages: []
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.handleChange = this.handleChange.bind(this);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.submitMessage = this.submitMessage.bind(this);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  handleChange(event) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.setState({
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      input: event.target.value
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  submitMessage() {
							 
						 
					
						
							
								
									
										
										
										
											2020-08-11 22:42:27 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    this.setState((state) => ({
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      input: '',
							 
						 
					
						
							
								
									
										
										
										
											2020-08-11 22:42:27 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      messages: state.messages.concat(state.input)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }));
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  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 > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Change code above this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const mapStateToProps = (state) => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return {messages: state}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const mapDispatchToProps = (dispatch) => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    submitNewMessage: (message) => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      dispatch(addMessage(message))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const Container = connect(mapStateToProps, mapDispatchToProps)(Presentational);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class AppWrapper extends React.Component {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  render() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return (
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      < Provider  store = {store} > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        < Container / > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      < / Provider > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-13 18:58:50 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```jsx
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// 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:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const Provider = ReactRedux.Provider;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const connect = ReactRedux.connect;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Change code below this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class Presentational extends React.Component {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  constructor(props) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    super(props);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.state = {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      input: ''
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2018-10-08 01:01:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								 this.handleChange = this.handleChange.bind(this);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 this.submitMessage = this.submitMessage.bind(this);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  handleChange(event) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.setState({
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      input: event.target.value
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  submitMessage() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.props.submitNewMessage(this.state.input);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.setState({
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      input: ''
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  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.props.messages.map( (message, idx) => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								              return (
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                 < li  key = {idx} > {message}< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								              )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            })
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        < / ul > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      < / div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Change code above this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const mapStateToProps = (state) => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return {messages: state}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const mapDispatchToProps = (dispatch) => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    submitNewMessage: (message) => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      dispatch(addMessage(message))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const Container = connect(mapStateToProps, mapDispatchToProps)(Presentational);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class AppWrapper extends React.Component {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  render() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return (
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      < Provider  store = {store} > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        < Container / > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      < / Provider > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```