Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve watch to reflect overloads #1056

Merged
merged 4 commits into from
Sep 21, 2024

Conversation

honey32
Copy link
Contributor

@honey32 honey32 commented Mar 29, 2024

relates to #1040

I tried to make description in https://react-hook-form.com/docs/useform/watch to reflect real overload of the function according to its JSDoc

I'm not sure about description, but I'm sure it would be better if organized like this, because it has two different usages, which, without explanation, may lead beginners to confusion.

I'm willing to make some additional edit if needed to make description more precise.

Copy link

vercel bot commented Mar 29, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-hook-form-website ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 21, 2024 7:47am

bluebill1049
bluebill1049 previously approved these changes Apr 2, 2024
Copy link
Member

@bluebill1049 bluebill1049 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Ty

@BrendanC23
Copy link

Does this address issue #1040? It's probably worth documenting the type parameter (which is an EventType). The documentation should also mention that calling setValue within the watch callback will trigger an infinite loop (which can be avoided by checking that type === "change" before setting the value).

Also, TypeScript gives this as the type for watch:

export type WatchObserver<TFieldValues extends FieldValues> = (value: DeepPartial<TFieldValues>, info: {
    name?: FieldPath<TFieldValues>;
    type?: EventType;
    values?: unknown;
}) => void;

What is info.values? It's typed as unknown, but contains the form values, just like value.


Slightly unrelated: Why is value typed as DeepPartial<TFieldValues>? In order to access any properties, I need to use optional chaining because everything might be undefined. Why not type it as TFieldValues instead, which is what getValues() returns?

Currently, I'm doing this to easily access nested properties:

useEffect(() => {
    const subscription = watch((value, { name, type }) => {
        const actualValues = getValues();
    });

    return () => subscription.unsubscribe();
}, [watch]);

@bluebill1049 bluebill1049 merged commit 5e1ddf2 into react-hook-form:master Sep 21, 2024
1 of 2 checks passed
@honey32 honey32 deleted the revise-watch-doc branch September 24, 2024 06:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants