Download react-hook-form/src/useWatch.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 8.99 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/src/useWatch.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/src/useWatch.ts
-
curl -L -o useWatch.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/src/useWatch.ts
8.99 kB
| import React from 'react'; | |
| import generateWatchOutput from './logic/generateWatchOutput'; | |
| import deepEqual from './utils/deepEqual'; | |
| import type { | |
| Control, | |
| DeepPartialSkipArrayKey, | |
| FieldPath, | |
| FieldPathValue, | |
| FieldPathValues, | |
| FieldValues, | |
| InternalFieldName, | |
| UseWatchProps, | |
| } from './types'; | |
| import { useFormContext } from './useFormContext'; | |
| import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'; | |
| /** | |
| * Subscribe to the entire form values change and re-render at the hook level. | |
| * | |
| * @remarks | |
| * | |
| * [API](https://react-hook-form.com/docs/usewatch) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-usewatch-h9i5e) | |
| * | |
| * @param props - defaultValue, disable subscription and match exact name. | |
| * | |
| * @example | |
| * ```tsx | |
| * const { control } = useForm(); | |
| * const values = useWatch({ | |
| * control, | |
| * defaultValue: { | |
| * name: "data" | |
| * }, | |
| * exact: false, | |
| * }) | |
| * ``` | |
| */ | |
| export function useWatch< | |
| TFieldValues extends FieldValues = FieldValues, | |
| TTransformedValues = TFieldValues, | |
| >(props: { | |
| name?: undefined; | |
| defaultValue?: DeepPartialSkipArrayKey<TFieldValues>; | |
| control?: Control<TFieldValues, any, TTransformedValues>; | |
| disabled?: boolean; | |
| exact?: boolean; | |
| compute?: undefined; | |
| }): DeepPartialSkipArrayKey<TFieldValues>; | |
| /** | |
| * Custom hook to subscribe to field change and isolate re-rendering at the component level. | |
| * | |
| * @remarks | |
| * | |
| * [API](https://react-hook-form.com/docs/usewatch) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-usewatch-h9i5e) | |
| * | |
| * @param props - defaultValue, disable subscription and match exact name. | |
| * | |
| * @example | |
| * ```tsx | |
| * const { control } = useForm(); | |
| * const values = useWatch({ | |
| * control, | |
| * name: "fieldA", | |
| * defaultValue: "default value", | |
| * exact: false, | |
| * }) | |
| * ``` | |
| */ | |
| export function useWatch< | |
| TFieldValues extends FieldValues = FieldValues, | |
| TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>, | |
| TTransformedValues = TFieldValues, | |
| >(props: { | |
| name: TFieldName; | |
| defaultValue?: FieldPathValue<TFieldValues, TFieldName>; | |
| control?: Control<TFieldValues, any, TTransformedValues>; | |
| disabled?: boolean; | |
| exact?: boolean; | |
| compute?: undefined; | |
| }): FieldPathValue<TFieldValues, TFieldName>; | |
| /** | |
| * Custom hook to subscribe to field change and compute function to produce state update | |
| * | |
| * @remarks | |
| * | |
| * [API](https://react-hook-form.com/docs/usewatch) | |
| * | |
| * @param props - defaultValue, disable subscription and match exact name. | |
| * | |
| * @example | |
| * ```tsx | |
| * const { control } = useForm(); | |
| * const values = useWatch({ | |
| * control, | |
| * compute: (formValues) => formValues.fieldA | |
| * }) | |
| * ``` | |
| */ | |
| export function useWatch< | |
| TFieldValues extends FieldValues = FieldValues, | |
| TTransformedValues = TFieldValues, | |
| TComputeValue = unknown, | |
| >(props: { | |
| name?: undefined; | |
| defaultValue?: DeepPartialSkipArrayKey<TFieldValues>; | |
| control?: Control<TFieldValues, any, TTransformedValues>; | |
| disabled?: boolean; | |
| exact?: boolean; | |
| compute: (formValues: TFieldValues) => TComputeValue; | |
| }): TComputeValue; | |
| /** | |
| * Custom hook to subscribe to field change and compute function to produce state update | |
| * | |
| * @remarks | |
| * | |
| * [API](https://react-hook-form.com/docs/usewatch) | |
| * | |
| * @param props - defaultValue, disable subscription and match exact name. | |
| * | |
| * @example | |
| * ```tsx | |
| * const { control } = useForm(); | |
| * const values = useWatch({ | |
| * control, | |
| * name: "fieldA", | |
| * defaultValue: "default value", | |
| * exact: false, | |
| * compute: (fieldValue) => fieldValue === "data" ? fieldValue : null, | |
| * }) | |
| * ``` | |
| */ | |
| export function useWatch< | |
| TFieldValues extends FieldValues = FieldValues, | |
| TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>, | |
| TTransformedValues = TFieldValues, | |
| TComputeValue = unknown, | |
| >(props: { | |
| name: TFieldName; | |
| defaultValue?: FieldPathValue<TFieldValues, TFieldName>; | |
| control?: Control<TFieldValues, any, TTransformedValues>; | |
| disabled?: boolean; | |
| exact?: boolean; | |
| compute: ( | |
| fieldValue: FieldPathValue<TFieldValues, TFieldName>, | |
| ) => TComputeValue; | |
| }): TComputeValue; | |
| /** | |
| * Custom hook to subscribe to field change and isolate re-rendering at the component level. | |
| * | |
| * @remarks | |
| * | |
| * [API](https://react-hook-form.com/docs/usewatch) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-usewatch-h9i5e) | |
| * | |
| * @param props - defaultValue, disable subscription and match exact name. | |
| * | |
| * @example | |
| * ```tsx | |
| * const { control } = useForm(); | |
| * const values = useWatch({ | |
| * control, | |
| * name: ["fieldA", "fieldB"], | |
| * defaultValue: { | |
| * fieldA: "data", | |
| * fieldB: "data" | |
| * }, | |
| * exact: false, | |
| * }) | |
| * ``` | |
| */ | |
| export function useWatch< | |
| TFieldValues extends FieldValues = FieldValues, | |
| TFieldNames extends | |
| readonly FieldPath<TFieldValues>[] = readonly FieldPath<TFieldValues>[], | |
| TTransformedValues = TFieldValues, | |
| >(props: { | |
| name: readonly [...TFieldNames]; | |
| defaultValue?: DeepPartialSkipArrayKey<TFieldValues>; | |
| control?: Control<TFieldValues, any, TTransformedValues>; | |
| disabled?: boolean; | |
| exact?: boolean; | |
| compute?: undefined; | |
| }): FieldPathValues<TFieldValues, TFieldNames>; | |
| /** | |
| * Custom hook to subscribe to field change and compute function to produce state update | |
| * | |
| * @remarks | |
| * | |
| * [API](https://react-hook-form.com/docs/usewatch) | |
| * | |
| * @param props - defaultValue, disable subscription and match exact name. | |
| * | |
| * @example | |
| * ```tsx | |
| * const { control } = useForm(); | |
| * const values = useWatch({ | |
| * control, | |
| * name: ["fieldA", "fieldB"], | |
| * defaultValue: { | |
| * fieldA: "data", | |
| * fieldB: 0 | |
| * }, | |
| * compute: ([fieldAValue, fieldBValue]) => fieldB === 2 ? fieldA : null, | |
| * exact: false, | |
| * }) | |
| * ``` | |
| */ | |
| export function useWatch< | |
| TFieldValues extends FieldValues = FieldValues, | |
| TFieldNames extends | |
| readonly FieldPath<TFieldValues>[] = readonly FieldPath<TFieldValues>[], | |
| TTransformedValues = TFieldValues, | |
| TComputeValue = unknown, | |
| >(props: { | |
| name: readonly [...TFieldNames]; | |
| defaultValue?: DeepPartialSkipArrayKey<TFieldValues>; | |
| control?: Control<TFieldValues, any, TTransformedValues>; | |
| disabled?: boolean; | |
| exact?: boolean; | |
| compute: ( | |
| fieldValue: FieldPathValues<TFieldValues, TFieldNames>, | |
| ) => TComputeValue; | |
| }): TComputeValue; | |
| /** | |
| * Custom hook to subscribe to field change and isolate re-rendering at the component level. | |
| * | |
| * @remarks | |
| * | |
| * [API](https://react-hook-form.com/docs/usewatch) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-usewatch-h9i5e) | |
| * | |
| * @example | |
| * ```tsx | |
| * // can skip passing down the control into useWatch if the form is wrapped with the FormProvider | |
| * const values = useWatch() | |
| * ``` | |
| */ | |
| export function useWatch< | |
| TFieldValues extends FieldValues = FieldValues, | |
| >(): DeepPartialSkipArrayKey<TFieldValues>; | |
| /** | |
| * Custom hook to subscribe to field change and isolate re-rendering at the component level. | |
| * | |
| * @remarks | |
| * | |
| * [API](https://react-hook-form.com/docs/usewatch) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-usewatch-h9i5e) | |
| * | |
| * @example | |
| * ```tsx | |
| * const { control } = useForm(); | |
| * const values = useWatch({ | |
| * name: "fieldName" | |
| * control, | |
| * }) | |
| * ``` | |
| */ | |
| export function useWatch<TFieldValues extends FieldValues>( | |
| props?: UseWatchProps<TFieldValues>, | |
| ) { | |
| const methods = useFormContext<TFieldValues>(); | |
| const { | |
| control = methods.control, | |
| name, | |
| defaultValue, | |
| disabled, | |
| exact, | |
| compute, | |
| } = props || {}; | |
| const _defaultValue = React.useRef(defaultValue); | |
| const _compute = React.useRef(compute); | |
| const _computeFormValues = React.useRef(undefined); | |
| _compute.current = compute; | |
| const defaultValueMemo = React.useMemo( | |
| () => | |
| control._getWatch( | |
| name as InternalFieldName, | |
| _defaultValue.current as DeepPartialSkipArrayKey<TFieldValues>, | |
| ), | |
| [control, name], | |
| ); | |
| const [value, updateValue] = React.useState( | |
| _compute.current ? _compute.current(defaultValueMemo) : defaultValueMemo, | |
| ); | |
| useIsomorphicLayoutEffect( | |
| () => | |
| control._subscribe({ | |
| name, | |
| formState: { | |
| values: true, | |
| }, | |
| exact, | |
| callback: (formState) => { | |
| if (!disabled) { | |
| const formValues = generateWatchOutput( | |
| name as InternalFieldName | InternalFieldName[], | |
| control._names, | |
| formState.values || control._formValues, | |
| false, | |
| _defaultValue.current, | |
| ); | |
| if (_compute.current) { | |
| const computedFormValues = _compute.current(formValues); | |
| if (!deepEqual(computedFormValues, _computeFormValues.current)) { | |
| updateValue(computedFormValues); | |
| _computeFormValues.current = computedFormValues; | |
| } | |
| } else { | |
| updateValue(formValues); | |
| } | |
| } | |
| }, | |
| }), | |
| [control, disabled, name, exact], | |
| ); | |
| React.useEffect(() => control._removeUnmounted()); | |
| return value; | |
| } | |