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

1.4 KiB
Raw Blame History

title localeTitle
Pass a Callback as Props 将回调作为道具传递

将回调作为道具传递

描述

  • GetInput组件添加到MyApp中的render方法然后将一个名为inpu的prop从inputValue的状态传递给inputValue 。还要创建一个名为handleChange的prop并将输入处理程序handleChange给它。
  • RenderInput添加到MyApp中的render方法然后创建一个名为input的prop并将inputValue从state传递给它。

提示

  • state是一个属性Myapp所以用“this.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> 
    ); 
  } 
 };