<分区>
如果您在 onClick 事件(或类似事件)上触发它,我知道如何让父函数运行,但我想在 ajax 成功时触发它。几乎就是这个星座:
var parent = React.createClass({
someFunction: function(){
console.log("Parent function triggered");
},
render: function(){
return (
<Child callback={this.someFunction} />
);
}
});
var child = React.createClass({
getInitialState: function(){
return { data: "Wat" };
},
componentDidMount: function(){
$.ajax({
url: "some_url",
method: 'POST',
data: this.state.data,
success: function(response){
this.props.callback; // This is what I would like to do
},
error: function(){
console.log("Couldn't do it cap'n");
}
});
},
render: function(){
return(
<div>Hello!</div>
);
}
});
我可以通过触发一个事件来做到这一点,但当我有权访问该功能时,肯定应该可以做到这一点。该函数也被正确传递,如果我执行 console.log(this.props.callback);