Passing data to parent component in react

后端 未结 1 474
夕颜
夕颜 2020-12-11 22:08

I\'ve created on form in child component. After submitting that form using jquery ajax method ($.ajax) I am retriving some data in json format. I want to put th

相关标签:
1条回答
  • 2020-12-11 22:52

    The way to do this without some kind of Flux implementation is to create a function on the parent element that handles the response/data from the child and pass that function as a prop. Then call that function from the child. Something like this:

    Parent:

    handleResponse(data) {
      console.log(data)
    }
    
    render() {
      return(
        <div>
          <Child handleResponse={this.handleResponse} />
        </div>
      );
    }
    

    then in the child:

    handleAjax() {
      $.get(url).then( (response) => {
        this.props.handleResponse(response)
      });
    }
    

    this all assumes ES6 syntax. Using ES5 you're going to have to use bind or var that = this to scope everything correctly.

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