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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…