1.4 KiB
1.4 KiB
title | localeTitle |
---|---|
Pass a Callback as Props | 将回调作为道具传递 |
将回调作为道具传递
描述
- 将
GetInput
组件添加到MyApp中的render方法,然后将一个名为inpu
的prop从inputValue
的状态传递给inputValue
。还要创建一个名为handleChange
的prop,并将输入处理程序handleChange
给它。 - 将
RenderInput
添加到MyApp中的render方法,然后创建一个名为input
的prop,并将inputValue
从state传递给它。
提示
解
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>
);
}
};