218 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
		
		
			
		
	
	
			218 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
| 
								 | 
							
								---
							 | 
						||
| 
								 | 
							
								id: 5a24c314108439a4d403617b
							 | 
						||
| 
								 | 
							
								title: Pass a Callback as Props
							 | 
						||
| 
								 | 
							
								challengeType: 6
							 | 
						||
| 
								 | 
							
								forumTopicId: 301400
							 | 
						||
| 
								 | 
							
								dashedName: pass-a-callback-as-props
							 | 
						||
| 
								 | 
							
								---
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								# --description--
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								You can pass `state` as props to child components, but you're not limited to passing data. You can also pass handler functions or any method that's defined on a React component to a child component. This is how you allow child components to interact with their parent components. You pass methods to a child just like a regular prop. It's assigned a name and you have access to that method name under `this.props` in the child component.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								# --instructions--
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								There are three components outlined in the code editor. The `MyApp` component is the parent that will render the `GetInput` and `RenderInput` child components. Add the `GetInput` component to the render method in `MyApp`, then pass it a prop called `input` assigned to `inputValue` from `MyApp`'s `state`. Also create a prop called `handleChange` and pass the input handler `handleChange` to it.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								Next, add `RenderInput` to the render method in `MyApp`, then create a prop called `input` and pass the `inputValue` from `state` to it. Once you are finished you will be able to type in the `input` field in the `GetInput` component, which then calls the handler method in its parent via props. This updates the input in the `state` of the parent, which is passed as props to both children. Observe how the data flows between the components and how the single source of truth remains the `state` of the parent component. Admittedly, this example is a bit contrived, but should serve to illustrate how data and callbacks can be passed between React components.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								# --hints--
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								The `MyApp` component should render.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								```js
							 | 
						||
| 
								 | 
							
								assert(
							 | 
						||
| 
								 | 
							
								  (function () {
							 | 
						||
| 
								 | 
							
								    const mockedComponent = Enzyme.mount(React.createElement(MyApp));
							 | 
						||
| 
								 | 
							
								    return mockedComponent.find('MyApp').length === 1;
							 | 
						||
| 
								 | 
							
								  })()
							 | 
						||
| 
								 | 
							
								);
							 | 
						||
| 
								 | 
							
								```
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								The `GetInput` component should render.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								```js
							 | 
						||
| 
								 | 
							
								assert(
							 | 
						||
| 
								 | 
							
								  (function () {
							 | 
						||
| 
								 | 
							
								    const mockedComponent = Enzyme.mount(React.createElement(MyApp));
							 | 
						||
| 
								 | 
							
								    return mockedComponent.find('GetInput').length === 1;
							 | 
						||
| 
								 | 
							
								  })()
							 | 
						||
| 
								 | 
							
								);
							 | 
						||
| 
								 | 
							
								```
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								The `RenderInput` component should render.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								```js
							 | 
						||
| 
								 | 
							
								assert(
							 | 
						||
| 
								 | 
							
								  (function () {
							 | 
						||
| 
								 | 
							
								    const mockedComponent = Enzyme.mount(React.createElement(MyApp));
							 | 
						||
| 
								 | 
							
								    return mockedComponent.find('RenderInput').length === 1;
							 | 
						||
| 
								 | 
							
								  })()
							 | 
						||
| 
								 | 
							
								);
							 | 
						||
| 
								 | 
							
								```
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								The `GetInput` component should receive the `MyApp` state property `inputValue` as props and contain an `input` element which modifies `MyApp` state.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								```js
							 | 
						||
| 
								 | 
							
								async () => {
							 | 
						||
| 
								 | 
							
								  const waitForIt = (fn) =>
							 | 
						||
| 
								 | 
							
								    new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 250));
							 | 
						||
| 
								 | 
							
								  const mockedComponent = Enzyme.mount(React.createElement(MyApp));
							 | 
						||
| 
								 | 
							
								  const state_1 = () => {
							 | 
						||
| 
								 | 
							
								    mockedComponent.setState({ inputValue: '' });
							 | 
						||
| 
								 | 
							
								    return waitForIt(() => mockedComponent.state());
							 | 
						||
| 
								 | 
							
								  };
							 | 
						||
| 
								 | 
							
								  const state_2 = () => {
							 | 
						||
| 
								 | 
							
								    mockedComponent
							 | 
						||
| 
								 | 
							
								      .find('input')
							 | 
						||
| 
								 | 
							
								      .simulate('change', { target: { value: 'TestInput' } });
							 | 
						||
| 
								 | 
							
								    return waitForIt(() => mockedComponent.state());
							 | 
						||
| 
								 | 
							
								  };
							 | 
						||
| 
								 | 
							
								  const updated_1 = await state_1();
							 | 
						||
| 
								 | 
							
								  const updated_2 = await state_2();
							 | 
						||
| 
								 | 
							
								  assert(updated_1.inputValue === '' && updated_2.inputValue === 'TestInput');
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								```
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								The `RenderInput` component should receive the `MyApp` state property `inputValue` as props.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								```js
							 | 
						||
| 
								 | 
							
								async () => {
							 | 
						||
| 
								 | 
							
								  const waitForIt = (fn) =>
							 | 
						||
| 
								 | 
							
								    new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 250));
							 | 
						||
| 
								 | 
							
								  const mockedComponent = Enzyme.mount(React.createElement(MyApp));
							 | 
						||
| 
								 | 
							
								  const state_1 = () => {
							 | 
						||
| 
								 | 
							
								    mockedComponent.setState({ inputValue: 'TestName' });
							 | 
						||
| 
								 | 
							
								    return waitForIt(() => mockedComponent);
							 | 
						||
| 
								 | 
							
								  };
							 | 
						||
| 
								 | 
							
								  const updated_1 = await state_1();
							 | 
						||
| 
								 | 
							
								  assert(updated_1.find('p').text().includes('TestName'));
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								```
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								# --seed--
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								## --after-user-code--
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								```jsx
							 | 
						||
| 
								 | 
							
								ReactDOM.render(<MyApp />, document.getElementById('root'))
							 | 
						||
| 
								 | 
							
								```
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								## --seed-contents--
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								```jsx
							 | 
						||
| 
								 | 
							
								class MyApp extends React.Component {
							 | 
						||
| 
								 | 
							
								  constructor(props) {
							 | 
						||
| 
								 | 
							
								    super(props);
							 | 
						||
| 
								 | 
							
								    this.state = {
							 | 
						||
| 
								 | 
							
								      inputValue: ''
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    this.handleChange = this.handleChange.bind(this);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  handleChange(event) {
							 | 
						||
| 
								 | 
							
								    this.setState({
							 | 
						||
| 
								 | 
							
								      inputValue: event.target.value
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  render() {
							 | 
						||
| 
								 | 
							
								    return (
							 | 
						||
| 
								 | 
							
								       <div>
							 | 
						||
| 
								 | 
							
								        { /* Change code below this line */ }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        { /* Change code above this line */ }
							 | 
						||
| 
								 | 
							
								       </div>
							 | 
						||
| 
								 | 
							
								    );
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class GetInput extends React.Component {
							 | 
						||
| 
								 | 
							
								  constructor(props) {
							 | 
						||
| 
								 | 
							
								    super(props);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  render() {
							 | 
						||
| 
								 | 
							
								    return (
							 | 
						||
| 
								 | 
							
								      <div>
							 | 
						||
| 
								 | 
							
								        <h3>Get Input:</h3>
							 | 
						||
| 
								 | 
							
								        <input
							 | 
						||
| 
								 | 
							
								          value={this.props.input}
							 | 
						||
| 
								 | 
							
								          onChange={this.props.handleChange}/>
							 | 
						||
| 
								 | 
							
								      </div>
							 | 
						||
| 
								 | 
							
								    );
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class RenderInput extends React.Component {
							 | 
						||
| 
								 | 
							
								  constructor(props) {
							 | 
						||
| 
								 | 
							
								    super(props);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  render() {
							 | 
						||
| 
								 | 
							
								    return (
							 | 
						||
| 
								 | 
							
								      <div>
							 | 
						||
| 
								 | 
							
								        <h3>Input Render:</h3>
							 | 
						||
| 
								 | 
							
								        <p>{this.props.input}</p>
							 | 
						||
| 
								 | 
							
								      </div>
							 | 
						||
| 
								 | 
							
								    );
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								```
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								# --solutions--
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								```jsx
							 | 
						||
| 
								 | 
							
								class MyApp extends React.Component {
							 | 
						||
| 
								 | 
							
								  constructor(props) {
							 | 
						||
| 
								 | 
							
								    super(props);
							 | 
						||
| 
								 | 
							
								    this.state = {
							 | 
						||
| 
								 | 
							
								      inputValue: ''
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								  this.handleChange = this.handleChange.bind(this);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  handleChange(event) {
							 | 
						||
| 
								 | 
							
								    this.setState({
							 | 
						||
| 
								 | 
							
								      inputValue: event.target.value
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  render() {
							 | 
						||
| 
								 | 
							
								    return (
							 | 
						||
| 
								 | 
							
								       <div>
							 | 
						||
| 
								 | 
							
								         <GetInput
							 | 
						||
| 
								 | 
							
								           input={this.state.inputValue}
							 | 
						||
| 
								 | 
							
								           handleChange={this.handleChange}/>
							 | 
						||
| 
								 | 
							
								         <RenderInput
							 | 
						||
| 
								 | 
							
								           input={this.state.inputValue}/>
							 | 
						||
| 
								 | 
							
								       </div>
							 | 
						||
| 
								 | 
							
								    );
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class GetInput extends React.Component {
							 | 
						||
| 
								 | 
							
								  constructor(props) {
							 | 
						||
| 
								 | 
							
								    super(props);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  render() {
							 | 
						||
| 
								 | 
							
								    return (
							 | 
						||
| 
								 | 
							
								      <div>
							 | 
						||
| 
								 | 
							
								        <h3>Get Input:</h3>
							 | 
						||
| 
								 | 
							
								        <input
							 | 
						||
| 
								 | 
							
								          value={this.props.input}
							 | 
						||
| 
								 | 
							
								          onChange={this.props.handleChange}/>
							 | 
						||
| 
								 | 
							
								      </div>
							 | 
						||
| 
								 | 
							
								    );
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class RenderInput extends React.Component {
							 | 
						||
| 
								 | 
							
								  constructor(props) {
							 | 
						||
| 
								 | 
							
								    super(props);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  render() {
							 | 
						||
| 
								 | 
							
								    return (
							 | 
						||
| 
								 | 
							
								      <div>
							 | 
						||
| 
								 | 
							
								        <h3>Input Render:</h3>
							 | 
						||
| 
								 | 
							
								        <p>{this.props.input}</p>
							 | 
						||
| 
								 | 
							
								      </div>
							 | 
						||
| 
								 | 
							
								    );
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								```
							 |