Jen*_*ins 4 forms components submit reactjs
有3个组件。第一个是名为 A 的父组件。其他是 X 和 Y。
X 是一个带有保存按钮的工具栏组件。而 Y 是一个具有一些输入的 Form 组件(显然)。并且它们都是在 A 中导入和渲染的。
所以我想要做的是当 X 中的保存按钮被点击时,我希望 B 中的表单被提交。
提前致谢。
You can communicate with the parent from child and vice versa.
What you need to do is pass a handler to Component X from Component A and then in this handler using refs you can access the Child component form and trigger a submit like
A:
class A extends React.Component {
constructor(props) {
super(props);
}
buttonSubmit = () => {
this.Yform.childForm.submit()
}
render() {
return <div>
<X triggerSubmit={this.buttonSubmit}/>
<Y ref={(form) => this.Yform = form}/>
</div>
}
}
Run Code Online (Sandbox Code Playgroud)
X
class X extends React.Component {
constructor(props) {
super(props);
}
render() {
return <div>
<button onClick={() => this.props.triggerSubmit()}>Submit</button>
</div>
}
}
Run Code Online (Sandbox Code Playgroud)
Y:
class Y extends React.Component {
constructor(props) {
super(props);
}
render() {
return <div>
<form ref={(childForm) => {this.childForm = childForm}}>
...
</form>
</div>
}
}
Run Code Online (Sandbox Code Playgroud)
In case you are using Redux, then you need to do call an onSubmit like
this.YForm.getWrappedInstance().submit()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3928 次 |
| 最近记录: |