替代 React Hook Form 的 useWatch

huangapple go评论81阅读模式
英文:

Alternate to React hook Form useWatch

问题

以下是您要翻译的内容:

"对于我的React应用程序,我正在使用React Hook Form(它是一个旧版本v5,没有useWatch函数)。

因此,我目前正在使用watch()来监视我的输入字段并在每次按键输入时更新我的全局状态。

由于我没有升级底层的React Hook Form版本的选项,是否有任何替代方法来优化使用watch()来监视输入字段的输入?

已更新

const inputField = watch(field.id);

const updateGlobalState = _.debounce(() => {
handleInputChange(field.id, inputField); // 这会更新父组件中的全局状态(上下文API对象),并使用更新后的值更新传递给表单的“defaultValues”
}, 300);

useMemo(() => {
setValue(field.id, props?.defaultValues?.[field.id]); // 我需要这样做,因为可能会出现其他字段的更新,比如下拉菜单会导致我清空该文本字段ID。
}, [props?.defaultValues]);

useMemo(() => {
updateGlobalState();
}, [inputField]);

上述内容的两个重要点:

  1. 调用handleInputChange会更新我的全局状态({field1: 'A', field2: 'B'}),我会使用更新后的值更新传递给表单的“defaultValues”。
  2. 我使用setValue,因为可能会出现其他字段的更新,比如下拉菜单会导致我清空该文本字段ID。"
英文:

For my React app, I am consuming React Hook Form (which is on on older version v5 which does not have useWatch)

So, I am currently using watch() to watch my input fields and update my global state on every key input.

Since I do not have an option to upgrade my underlying React Hook Form version, is there any alternate way to optimize the use of watch() for input field entry ?

UPDATED

const inputField = watch(field.id);

const updateGlobalState = _.debounce(() => {
	handleInputChange(field.id, inputField); // This updates the global state (context api obj) in parent component and I update my "defaultValues" passed to the form with the updated value
}, 300);

useMemo(() => {
	setValue(field.id, props?.defaultValues?.[field.id]); // I need this, since there can be use cases where updates in some other field say dropdown causes me to empty out this text field id.
}, [props?.defaultValues]);

useMemo(() => {
		updateGlobalState();	
}, [inputField]);

Two important points from above;

  1. Call to handleInputChange updates my global state ({field1: 'A', field2: 'B'}) and I update my "defaultValues" passed to the form with the updated value
  2. I use setValue, since there can be use cases where updates in some other field say a dropdown causes me to empty out this text field id.

答案1

得分: 1

是的,有几种方法可以优化在 React Hook Form v5 中使用 watch()

  1. 延迟处理输入:延迟处理是一种技术,它会在用户停止输入一段时间后才开始处理输入。可以使用 lodash 的 debounce 函数或类似的实现来实现这个目的。这样,你就不会在每次按键时更新全局状态,而是在用户停止输入一段时间后再进行更新。

  2. 尽可能使用 getValues(field) 而不是 watch(field):如果你只在特定时刻(比如表单提交时)需要字段的值,可以使用 getValues(field) 而不是 watch(field)getValues(field) 不会在字段变化时触发重新渲染,因此如果你不需要实时跟踪字段的值,这样做会更有效率。

import React, { useEffect } from 'react';
import { useForm } from 'react-hook-form';
import _ from 'lodash';

function MyForm() {
  const { register, watch, getValues } = useForm();
  const firstName = watch('firstName');
  const lastName = watch('lastName');

  // 延迟处理更新全局状态的函数
  const updateGlobalState = _.debounce((firstName, lastName) => {
    // 在这里更新全局状态
  }, 300);

  useEffect(() => {
    // 只有当两个字段都填写时才更新全局状态
    if (firstName && lastName) {
      updateGlobalState(firstName, lastName);
    }
  }, [firstName, lastName]);

  return (
    <form>
      <input name="firstName" ref={register} />
      <input name="lastName" ref={register} />
    </form>
  );
}
英文:

Yes, there are a few ways to optimize the use of watch() in React Hook Form v5.

  1. Debounce the input: Debouncing is a technique where you delay processing the input until the user has stopped typing for a certain amount of time. This can be done using lodash's debounce function or a similar implementation. This way, you're not updating the global state on every keystroke, but rather after the user has stopped typing for a certain amount of time.

  2. Use getValues(field) instead of watch(field) where possible: If you only need the value of a field at a specific point in time (like when the form is submitted), you can use getValues(field) instead of watch(field). getValues(field) doesn't cause a re-render when the field changes, so it's more efficient if you don't need to keep track of the field's value in real-time.

import React, { useEffect } from &#39;react&#39;;
import { useForm } from &#39;react-hook-form&#39;;
import _ from &#39;lodash&#39;;

function MyForm() {
  const { register, watch, getValues } = useForm();
  const firstName = watch(&#39;firstName&#39;);
  const lastName = watch(&#39;lastName&#39;);

  // Debounce the function that updates the global state
  const updateGlobalState = _.debounce((firstName, lastName) =&gt; {
    // Update the global state here
  }, 300);

  useEffect(() =&gt; {
    // Only update the global state if both fields are filled out
    if (firstName &amp;&amp; lastName) {
      updateGlobalState(firstName, lastName);
    }
  }, [firstName, lastName]);

  return (
    &lt;form&gt;
      &lt;input name=&quot;firstName&quot; ref={register} /&gt;
      &lt;input name=&quot;lastName&quot; ref={register} /&gt;
    &lt;/form&gt;
  );
}

huangapple
  • 本文由 发表于 2023年6月18日 22:24:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定