freeCodeCamp/guide/english/certifications/front-end-libraries/react/pass-a-callback-as-props/index.md

1.5 KiB

title
Pass a Callback as Props

Pass a Callback as Props

Description

  • 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.
  • Add RenderInput to the render method in MyApp, then create a prop called input and pass the inputValue from state to it.

Hints

Solution

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 */ 
        <GetInput input={this.state.inputValue} handleChange={this.handleChange}/>
        }
        { /* change code above this line */ 
        <RenderInput input={this.state.inputValue}/>
        }
       </div>
    );
  }
};