passing data from child to parent component - react - via callback function

后端 未结 6 1376
花落未央
花落未央 2020-12-06 07:40

passing data from child to parent component via callback function but somehow it\'s not working. what am I doing wrong here? passing data from child to parent component - re

6条回答
  •  生来不讨喜
    2020-12-06 08:13

    There are a couple of issues.

    1) You have to bind passingProps

    constructor(){
        super();
        this.state={
          text:''
        }
        this.passingProps = this.passingProps.bind(this);
    }
    

    2) this.setState is asynchronous, so it's not guaranteed that this.state.text will be set to the value you want by the time you pass it to this.props.getInput. You can either do

    this.props.getInput(newInput)
    

    or

    this.setState({ text: newInput }, () => {
      this.props.getInput(this.state.text);
    })
    

    to resolve that issue.

提交回复
热议问题