在这里您可以轻松地看到一种在父级上从子级运行函数的方法
http://andrewhfarmer.com/component-communication/#3-callback-functions
但是你会如何从子函数中传递参数呢?
最佳答案
如果你需要父组件的状态,你必须在传递它之前将他的 this 绑定(bind)到函数。
class Parent extends React.Component {
callMeMaybe(param) {
console.log(param)
}
render(){
return (
<TheChild callMeMaybe={this.callMeMaybe.bind(this)} />
)
}
}
class TheChild extends React.Component {
render(){
return (
<div>
<button onClick={ event => {
this.props.callMeMaybe("message from child") }
}>Send a Message to Parent </button>
</div>
)
}
}
这是一个jsfiddle给你玩。单击按钮后检查控制台。
关于javascript - 使用 prop 回调将参数从子级传递到父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39693873/