简体   繁体   English

如何重置拆分为不同部分的 react redux 表单(向导表单)?

[英]How to reset a react redux form split in different parts (Wizard form)?

I cannot manage to reset my react redux form that is split in 3 different forms/pages.我无法重置我的 react redux 表单,该表单分为 3 个不同的表单/页面。 It's a wizard form and I have been trying many ways to reset it unsuccessfully.这是一个向导表单,我一直在尝试多种方法来重置它,但未成功。

I've been using these 2 resources: How to build a wizard form: https://redux-form.com/8.3.0/examples/wizard/ How to reset a form: https://redux-form.com/6.0.0-alpha.4/docs/faq/howtoclear.md/我一直在使用这两个资源:如何构建向导表单: https : //redux-form.com/8.3.0/examples/wizard/如何重置表单: https : //redux-form.com/ 6.0.0-alpha.4/docs/faq/howtoclear.md/

However, it seems clear to me now that it kinda impossible to do so.然而,现在看来很清楚,我认为它有点不可能这样做。

I have 3 pages like so: WizardForm.jsx我有 3 个这样的页面:WizardForm.jsx

<div className="wizard__form-wrapper">
  {page === 1
  && (
    <WizardFormOne
      onSubmit={this.nextPage}
      initialValues={initialValues}
    />
  )}
  {page === 2
  && (
    <WizardFormTwo
      previousPage={this.previousPage}
      onSubmit={this.nextPage}
    />
  )}
  {page === 3
  && (
    <WizardFormThree
      previousPage={this.previousPage}
      onSubmit={values => onSubmit(values)}
    />
  )}
</div>

and on each form I'm exporting my form like so: WizardFormOne.jsx在每个表单上,我都像这样导出表单:WizardFormOne.jsx

export default reduxForm({
  form: 'wizard', //                 <------ same form name
  destroyOnUnmount: false, //        <------ preserve form data
  forceUnregisterOnUnmount: true, // <------ unregister fields on unmount
  validate,
})(WizardFormOne);

The form works great but I cannot reset it.该表格效果很好,但我无法重置它。 I've been trying all 4 options described in the link above.我一直在尝试上面链接中描述的所有 4 个选项。 FOr example, in my WizardForm.jsx I do:例如,在我的 WizardForm.jsx 中,我这样做:

componentWillUnmount() {
    const { dispatch } = this.props;
    dispatch(reset('wizard'));
  }

But nothing happens, and my form is not cleared.但是没有任何反应,我的表格没有被清除。

Any help is much appreciated.任何帮助深表感谢。 Thank you.谢谢你。

For what its worth I found a solution to this issue:对于它的价值,我找到了解决这个问题的方法:

You can pass a prop such as resetForm to the child component that contains the form, in m case it's WizardFormOne.jsx and do something like this:您可以将诸如 resetForm 之类的道具传递给包含该表单的子组件,如果它是 WizardFormOne.jsx 并执行如下操作:

constructor(props) {
    super(props);
    if (props.resetForm && props.isPristine) {
      props.destroy();
    }
  }

I'm sure you can put it in componentDidMount I'll have to keep working on it.我相信你可以把它放在 componentDidMount 中,我必须继续努力。 That did it in my case.在我的情况下做到了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM