1.5 KiB
1.5 KiB
title
| title |
|---|
| Pass a Callback as Props |
Pass a Callback as Props
Description
- Add the
GetInputcomponent to the render method in MyApp, then pass it a prop calledinput assigned toinputValuefrom MyApp's state. Also create a prop calledhandleChangeand pass the input handlerhandleChangeto it. - Add
RenderInputto the render method in MyApp, then create a prop calledinputand pass theinputValuefrom state to it.
Hints
stateis a property ofMyappclass, so use 'this.state' to get the object value- To learn more about state and props, read State and Lifecycle and Components and Props.
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>
);
}
};