ReactJS:来自父组件中子组件的触发事件

时间:2019-12-25 09:34:33

标签: reactjs

我有两个组成部分:1.父母2.孩子 子组件中有一个事件称为onChange(),该事件返回一个值。 我想接收从父组件的componentDidMount()中的OnChange()返回的值。

示例:

class Parent extends PureComponent {
componentDidMount() {
 let value = CHILD.onChange(); //triggered when ever onChange() 
}
 render(){
      return(
        <Child />
)
  }
}
const Child = () => {

  const onChange = () => {
    const value = 1
    return value;
  };
}

1 个答案:

答案 0 :(得分:1)

class Parent extends PureComponent {


handleChildChange = value => {
 //Do your stuff with value, pass it to the state and take it from there if you like
}


 render(){
      return(
        <Child handleChange={this.handleChildChange} />
)
  }
}
const Child = (props) => {

  const onChange = () => {
    value = 1
    props.handleChange(value);
    }
  };
}