简体   繁体   English

React hook Form 知道值何时被修改

[英]React hook Form know when value is modified

How can we know form is modified or not in react-hook-form.我们如何知道表单是否在 react-hook-form 中被修改。 Anyone have idea.任何人都有想法。 I want know if any of the value is changed and update the state edited to true.我想知道是否更改了任何值并将 state 编辑为 true。

After i provide the defaultValue to useForm({defaultValues:values}).在我将 defaultValue 提供给 useForm({defaultValues:values}) 之后。 I want to notified when the values is updated or changed from defaultValue.我想在值从 defaultValue 更新或更改时得到通知。

Use isDirty property使用isDirty属性

function YourComponent() {
 const { formState } = useForm();
 const isFormEdited = formState.isDirty;
 ....

Here is the docs reference这是文档参考

You can use dirtyFields from formstate also, check the length of the fields modified.您也可以使用formstate中的dirtyFields,检查修改字段的长度。

const [isEdited, setIsEdited] = useState(false);
const fieldsEdited = formState.dirtyFields;
useEffect(() => {
    if (Object.keys(fieldsEdited).length > 0) {
        setIsEdited(true);
    } else {
        setIsEdited(false);
    }
}, [fieldsEdited]);

Figured out how to accomplished this.想出了如何做到这一点。 It is important to specify the defaultValues property.指定 defaultValues 属性很重要。

See the isDirty section of formState documentation :请参阅 formState文档的 isDirty 部分:

Make sure to provide all inputs' defaultValues at the useForm, so hook form can have a single source of truth to compare whether the form is dirty.确保在 useForm 中提供所有输入的 defaultValues,这样钩子表单可以有一个单一的事实来源来比较表单是否脏。

And a short example:还有一个简短的例子:

  const {
    formState: {
      isDirty, 
    },
  } = useForm<ProfileFormInterface>({
    defaultValues: {name: 'John Doe', email: 'jd@johndoe.com'},
  })

isDirty will now only be true if the name or email changes. isDirty现在仅在名称或 email 更改时才为真。

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

相关问题 如何在 React Hook Form 上提交时增加更多价值? - How to add more value when we submit on React Hook Form? 交换或删除组时,React Hook Form 值不会改变 - React Hook Form value is not changing when swapping or Deleting Groups useEffect 在创建编辑表单时不更新 react-hook-form 的默认值 - useEffect is not updating the default value of react-hook-form when creating edit form 使用 object 作为 `react-hook-form` 中的值 - Using object as a value in `react-hook-form` React Hook Form 将控制器(值)重置为默认值 - React Hook Form reset Controller (value) to default React 表单挂钩 setValue 覆盖占位符值 - React form hook setValue overwrites placeholder value 提交表单时React useState钩子不起作用 - React useState hook is not working when submitting form React Hook Form:当另一个字段的值为'TEST'时如何根据需要设置一个字段 - React Hook Form : how to set a field as required when a value of another field is 'TEST' 得到了'DatePickerProps<unknown> ': onChange, value, renderInput' 尝试使用 MUI 和 react-hook-form 创建自定义组件时</unknown> - got 'DatePickerProps<unknown>': onChange, value, renderInput' when trying to create custom component with MUI and react-hook-form 支持在使用 React Hook 表单验证时更改另一个字段值的回调 - Support callback for changing another field value when using React Hook Form validation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM