<分区>
我有一个子组件,它是一个 Redux 表单,从它的 handleSubmit 方法,我需要在父组件上调用一个方法。我尝试通过将回调作为 Parent 的 Prop 传递来做到这一点,但没有任何反应。
我已经看到只有在子组件上使用事件处理程序直接调用函数时,此方法才有效。
import Parent from './parent.js';
class Child extends React.Component {
constructor(props) {
super(props);
};
callCloseModal = () => {
this.props.closeModal();
}
handleFormSubmit (values) {
this.callCloseModal()
}
render() {
<form onSubmit=
{handleSubmit(this.handleFormSubmit.bind(this))}>
.....
</form>
}
}
class Parent extends React.Component {
constructor (props) {
super(props)
this.state = {
modalOpen: false,
}
}
.....
handleModalClose() {
this.setState({ modalOpen: false })
}
render() {
<Child closeModal={this.handleModalClose}> {this.props.children}</Child>
}
}
如何从子组件的方法调用父组件的方法?
编辑:该方法是正确的,但它更高一级(祖 parent 组件)