简体   繁体   English

react-hook-form V7:卸载其注册组件后未清除值

[英]react-hook-form V7: value is not cleared after unmounting its registered component

In V6 when I toggled a component off, using the watch method, the component will be unmounted and so will any sibling that depends on the unmounted components' value:在 V6 中,当我使用watch方法关闭组件时,组件将被卸载,任何依赖于卸载组件值的兄弟姐妹也将被卸载:

For example:例如:

  • We have three components <ComponentA /> , <ComponentB /> and <ComponentC /> .我们有三个组件<ComponentA /><ComponentB /><ComponentC />
  • When <ComponentA /> equals 'Yes', <ComponentB/> is rendered.<ComponentA />等于 'Yes' 时,会渲染<ComponentB/>
  • When <ComponetB /> equals 1 or 2, <ComponentC /> is rendered.<ComponetB />等于 1 或 2 时,会渲染<ComponentC />
  • Finally, if I change <ComponentA /> to 'No' <ComponentB /> and <ComponentC /> will be unmounted and their values will be cleared.最后,如果我将<ComponentA />更改为“否” <ComponentB />并且<ComponentC />将被卸载并且它们的值将被清除。

Working example here https://codesandbox.io/s/mui-rfhv6-001-xy39c这里的工作示例https://codesandbox.io/s/mui-rfhv6-001-xy39c

On the other hand, in V7 when I change <ComponentA /> to 'No', <ComponentB /> is unmounted but its value is not cleared.另一方面,在 V7 中,当我将<ComponentA />更改为“否”时, <ComponentB />已卸载,但其值未清除。 Therefore, <ComponentC /> is not unmounted.因此, <ComponentC />没有被卸载。

Working example here: https://codesandbox.io/s/mui-rhfv7-001-rpg21此处的工作示例: https://codesandbox.io/s/mui-rhfv7-001-rpg21

I have tried setting shouldUnregister: true as shown below but, still get the same behavior.我已经尝试设置shouldUnregister: true如下所示,但仍然得到相同的行为。

const { control, watch, handleSubmit } = useForm({
  mode: "all",
  shouldUnregister: true
});

What am I missing?我错过了什么?

It's a breaking change from version 7. From the migration guide :这是版本 7 的重大更改。来自迁移指南

Important: input value and reference will no longer get removed after unmount.重要提示:卸载后输入值和引用将不再被删除。

The solution is to detect when the component should be unmounted and call unregister() to clear the field manually:解决方案是检测何时应卸载组件并调用unregister()手动清除该字段:

const { ...props, unregister } = useForm();

useEffect(() => {
  if (watchRadio !== "Yes") {
    unregister("numOfSup");
  }
}, [watchRadio, unregister]);

You can see the issue about the subject here .您可以在此处查看有关该主题的问题。 Search for shouldUnregister to see the relevant info.搜索shouldUnregister以查看相关信息。

Live Demo现场演示

编辑 67327916/react-hook-form-v7-key-value-in-fromstate-not-cleared-after-unmounting-its-regi

Thank you @NearHuscarl谢谢@NearHuscarl

Also, I found a simpler way to fix the issue with the help of the one of react-hook-form creators.此外,在 react-hook-form 创建者的帮助下,我找到了一种更简单的方法来解决问题。 Upgrading its version.升级其版本。 In my case I was using version 7.0.5 and upgraded to 7.3.5.就我而言,我使用的是 7.0.5 版本并升级到 7.3.5。

The benefit of this approach is that you don't have to detect when the component should be unmounted and call unregister() to clear the field manually.这种方法的好处是您不必检测何时应该卸载组件并调用unregister()手动清除该字段。

FYI, for future reference, here is the working CSD:仅供参考,这里是工作 CSD:

Live Demo 2现场演示 2

编辑 67327916/react-hook-form-v7-key-value-in-fromstate-not-cleared-after-unmounting-its-regi

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

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