Passing data to the parent component in response

I created a form in a child component. After submitting this form using jQuery ajax method, ($.ajax)I return some data in json format. I want to put this data in the state of the parent component. So, is there any method in response.js to pass a value or data from a child component to its parent component?

Thanks..

+4
source share
1 answer

The way to do this without any Flux implementation is to create a function on the parent element that processes the response / data from the child element and passes this function as a support. Then call this function from the child. Something like that:

:

handleResponse(data) {
  console.log(data)
}

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

child:

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

ES6. ES5, bind var that = this .

+11

Source: https://habr.com/ru/post/1607861/


All Articles