这里我有了父子通信,它工作得很好。
var ParentComponent = React.createClass({
update: function() {
console.log("updated!");
},
render: function() {
<ChildComponent callBack={this.update} />
}
})
var ChildComponent = React.createClass({
preupdate: function() {
console.log("pre update done!");
},
render: function() {
<button onClick={this.props.callback}>click to update parent</button>
}
})在button Onclick事件中,如何在调用父回调方法之前调用子组件中的preupdate函数。
发布于 2018-04-10 03:11:51
您可以调用preupdate函数作为单击处理程序。然后,当它完成时,它可以调用回调。
var ParentComponent = React.createClass({
update: function() {
console.log("updated!");
},
render: function() {
<ChildComponent callBack={this.update} />
}
})
var ChildComponent = React.createClass({
preupdate: function() {
console.log("pre update done!");
this.props.callback()
},
render: function() {
<button onClick={this.preupdate.bind(this)}>click to update parent</button>
}
})发布于 2020-04-21 12:18:23
react钩子可以参考下面的解决方案。
ProfilePage;
const AlertModal = (props) =>{ const goBack = () => { props.callBack('hello' );//可以在这里传递回调数据} return ( <>返回 );}导出默认AlertModal;
发布于 2018-04-10 03:11:12
使用以下内容:
<button onClick={(args)=>{this.preupdate(args);this.props.callback(args);}>click to update parent</button>您还需要重新定义preupdate函数,如下所示:
preupdate: ()=> {
console.log("pre update done!");
}以便在调用此方法时保留this上下文。
https://stackoverflow.com/questions/49739997
复制相似问题