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

后端 未结 6 1360
花落未央
花落未央 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 07:57
    class App extends React.Component{
    constructor(props){
      super(props);
      this.state={
        input:'this is the input for now'
      }
      this.handleInput=this.handleInput.bind(this);   
    }
    
    handleInput(event){
      let value = event.target.value;
      this.setState({
        input:value
      });
    }
    
    render(){
       return(
         <div>
           <h1>{this.state.input}</h1>
           <Child getInput={this.handleInput} />
         </div>
       );
      }
    }
    
     class Child extends React.Component{
       constructor(){
          super(props);
    }
    
    render(){
       return(
         <div>
         <input type="text" placeholder="please input a name..." onChange={this.props.getInput} />
        </div>
    
         )
       }
    }
    
    ReactDOM.render(
      <App/>,
      document.getElementById('app')
    );
    

    Here is the answer for your question. I hope your proplem is solved.

    0 讨论(0)
  • 2020-12-06 08:00

    In your Child Component, you have written following code:

    passingProps(e){
      var newInput=e.target.value;
      //alert(newInput);
      this.setState({
         text:newInput
      });
      this.props.getInput(this.state.text);
    }
    

    The issue is due to the asynchronous behaviour of setState function. It means you can not call setState on one line and expect its updates on next line. Use the callback function of setState to call the function of parent component just like this:

    passingProps(e){
      var newInput=e.target.value;
      //alert(newInput);
      this.setState({ text: newInput }, () => {
         this.props.getInput(this.state.text);
      })
    }
    

    Same thing is happening in handleInput function of App component.

    0 讨论(0)
  • this is not automatically bound in your passingProps function. Try arrow function syntax to bind it.

    passingProps = e => {
      var newInput=e.target.value;
      //alert(newInput);
      this.setState({
        text:newInput
      });
      this.props.getInput(this.state.text);
    }
    
    0 讨论(0)
  • 2020-12-06 08:08

    Two things that you need to correct it:

    1. if you want to access new state, you don't use this.state.input after this.setState({input: 'xxx'}). Here is reason why not it.
    2. this.passingProps = this.passingProps.bind(this) is defined what this is current scope. when you use this in component's function, this need to be bind.

    Changed codepen

    0 讨论(0)
  • 2020-12-06 08:12

    You can create a method in parent that accepts some data and then sets the received data as parent state. Then pass this method to child as props. Now let the method accept child state as input and then let the method set the received child state as parent state.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题