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

update react-hook-form column in comparision table #673

Merged
merged 6 commits into from
Apr 26, 2024

Conversation

AhmedBaset
Copy link
Contributor

@AhmedBaset AhmedBaset commented Apr 13, 2024

It's better to review this in unified mode with wide screen or scale down

@AhmedBaset AhmedBaset marked this pull request as draft April 13, 2024 03:15
@tannerlinsley
Copy link
Collaborator

Do we actually know that hook form is fully inferred all the way down to nested and deep fields?

| Granular reactivity | ✅ | ❓ | ❓ | ❓ | ❓ |
| Nested object/array fields | ✅ | ✅ | ❓ | | ❓ |
| Nested object/array fields | ✅ | ✅ | ❓ | | ❓ |
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I could put a tooltip here, I'd say while it suppors nested arrays but useFeildArray supports one level nesting

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can use a *(1) callout with a corresponding footnote

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I initially thought it was unsupported, but after rechecking the documentation, it appears to be a TypeScript issue.

for nested field array, you will have to cast the field array by its name.

const { fields } = useFieldArray({ name: `test.${index}.keyValue` as 'test.0.keyValue' });

https://react-hook-form.com/docs/usefieldarray

Copy link
Member

Choose a reason for hiding this comment

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

Let's add the *(1) callout here, then I'm okay to merge this :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it still needed after 👇?

I initially thought it was unsupported, but after rechecking the documentation, it appears to be a TypeScript issue.

for nested field array, you will have to cast the field array by its name.

const { fields } = useFieldArray({ name: `test.${index}.keyValue` as 'test.0.keyValue' });

https://react-hook-form.com/docs/usefieldarray

Copy link
Member

Choose a reason for hiding this comment

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

That's the * I'm hoping for - type casting is an obtuse way of supporting this that we don't require

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See 99a07ff (#673)

BTW, feel free to edit it yourself

@AhmedBaset
Copy link
Contributor Author

Do we actually know that hook form is fully inferred all the way down to nested and deep fields?

Yeah. When you pass a generic it infer everything from it:

const form = useForm<FormType>({
  // all props here infer FormType
});
//       ^? UseFormReturn<FormType>

all method and values of form are type safe

@AhmedBaset AhmedBaset marked this pull request as ready for review April 13, 2024 03:23
@tannerlinsley
Copy link
Collaborator

Generic casting isn’t ideal. How does it do with actual inference?

@AhmedBaset
Copy link
Contributor Author

one trade of is with arrays:

let's say you have

interface FormType {
  array: { key: string }[]
}

The way you access the key is by "array.0.key". It won't suggest you this but it will error if you didn't follow the shape. In other words array.whatever.key will case a TS error. But Ctrl+space will not suggest array.0.key.

@AhmedBaset
Copy link
Contributor Author

Generic casting isn’t ideal. How does it do with actual inference?

image

form.trigger("c") is TS error

const { name, onChange, onBlur, ref } = form.register("age", {
    validate: async (value, _formValues) => {
      await new Promise((resolve) => setTimeout(resolve, 1000));
      return value > 12 ? true : "error message";
    },
  });
| Nested object/array fields | ✅ | ✅ | ❓ | | ❓ |
| Async validation | ✅ | ✅ | ❓ | | ❓ |
| Nested object/array fields | ✅ | ✅ | ❓ | | ❓ |
| Async validation | ✅ | ✅ | ❓ | | ❓ |
Copy link
Contributor Author

Choose a reason for hiding this comment

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

const { name, onChange, onBlur, ref } = form.register("a", {
    validate: async (value, _formValues) => {
      await new Promise((resolve) => setTimeout(resolve, 1000));
      return value === "error" ? "error message" : undefined /* or true */;
    },
  });

@crutchcorn crutchcorn merged commit c3a2fd2 into TanStack:main Apr 26, 2024
@crutchcorn
Copy link
Member

Thanks for this!

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.

None yet

3 participants