【问题标题】:DefaultValues from useForm is not updated correctly with useFormContextuseForm 中的 DefaultValues 未使用 useFormContext 正确更新
【发布时间】:2021-08-02 09:18:54
【问题描述】:

参见示例:https://codesandbox.io/s/sad-franklin-d4o5z

在 useForm 中设置的默认值本身可以正常工作。但是,当它与 useFormContext 一起使用时,它无法正常工作;因为它只需要第一次初始化。价值。

在示例中,当对话框弹出时,目标值更改为“test”,我希望该值也被“test”填充。但是,它没有用。

【问题讨论】:

    标签: reactjs forms react-hooks react-hook-form


    【解决方案1】:

    通过useForm 设置defaultValue 只会在初始渲染时设置表单值。它的行为类似于useState,也只会在第一次渲染时使用传递的参数。

    所以你有两个选择:

    • 仅当 detailOpen 设置为 true

    • 使用来自 RHF 的 reset

    您的<Details /> 组件中还有一个小错字。您将<Controller /> name 属性设置为“number”而不是“name”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      • 2018-03-02
      • 1970-01-01
      • 2021-11-03
      • 2018-01-20
      相关资源
      最近更新 更多