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
issue: Error message have wrong type when using useFormContext #8653
Comments
hey @alveshelio
ref: https://github.com/react-hook-form/react-hook-form/blob/master/src/useFormContext.tsx#L38 - useFormContext()
+ useFormContext<FormValues>() // or useFormContext<any>() |
This will get patched in the next release, however, without a generic provided, we really can't do any type check for your form. Hope the above makes sense as well. |
If you show me exactly where I should provide a generic, I will take a look.
But the fact remains that in 2.32.0 everything works fine, and in 2.33.1
everything is broken. If that's not a problem in your eyes, then I will
definitely need to look elsewhere for a React form library.
…On Fri, Jul 8, 2022 at 4:34 PM Bill ***@***.***> wrote:
This will get patched in the next release, however, without a generic
provided, we really can't do any type check for your form. Hope the above
makes sense as well.
—
Reply to this email directly, view it on GitHub
<#8653 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAA7ZLV4I5LOJ5SM22W5Z73VTC3HRANCNFSM53CJQEFA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
@craigmcc please look elsewhere if this block you. |
Hi @bluebill1049, Thank you for getting back. I've tried with Again, I appreciate your help in this. |
hey, @alveshelio Would it be possible to provide a type definition for your form for the However, as you can see there is no type check for errors, it's literally
Not ideal, but doing this is probably better By looking at the issue a bit deeper, looks like without type definition then casting to string is required, this is simply due to without generic being provided for the type check. |
Hey @bluebill1049, I really can't thank you enough for being to quick and so helpful. I've made a donation a couple of weeks ago and I know that it is not near enough for all the great work you put into react-hook-form but it's a small gesture to encourage people like you who put so much of your time into open source and I think some times we forget how much these open source projects helps us. Once again, a big thank you. Regarding providing a type definition, in the codesandbox I've provided, if I pass const ControlInput = <T extends FieldValues>() => {
const {
register,
formState: { errors }
} = useFormContext<T>();
return (
<FormControl id="firstName" isInvalid={!!errors["firstName"]}>
<Input {...register("firstName")} placeholder="First Name" />
<FormErrorMessage>{errors["firstName"]?.message}</FormErrorMessage>
</FormControl>
);
}; |
I think it's probably not possible to infer the type through a generic by extending
At the moment, I do feel the pain that may cause to users who didn't provide a generic for |
I have reverted the previous two commits made for this issue as they are not really addressing this issue. |
Thank you, I'm fine for the time being to use |
Thanks for understanding @alveshelio I have updated the changelog to point this out, I am pretty excited for the next feature for |
* V8: feature: useFieldArray rules props (#8102) * useFieldArray rules prop * remove required prop * combine logic within validateField * remove logic * update unset method to support root node in array * update api extrator * update type * update useEffect validation logic * update api extrator * fix submit field array error * extract logic into its own function for update root error object * 8.0.0-alpha.3 * update with unit tests coverage for rules validate, minLength and maxLength * include support for required prop for consitent API and test coverage * update API * save some bytes * upgrade to react 18 * Revert "upgrade to react 18" This reverts commit d1c3bf7. * update react * fix cypress test * remove render count * udpate package and include test coverage for nested field array * fix unit and cypress tests * update lock file * revert package * avoid react native breaking change and fix test * fix test and type error * 🚔 close #8653 when useFormContext provide no generic for type check (#8654) * close #8653 when useFormContext provide no generic for type check * update api contract * 🛰 useFormContext include type tests (#8656) * Revert "🚔 close #8653 when useFormContext provide no generic for type check (#8654)" This reverts commit 2e0c3f8. * Revert "🛰 useFormContext include type tests (#8656)" This reverts commit 34af5b3. * 🥼 update changelog related to #8653 * 7.34.0-next.0 * check validation required for field array if not array or empty length * include unit tests coverage for validate fields for array fields * update api extrator
Version Number
7.33.1
Codesandbox/Expo snack
https://codesandbox.io/s/react-hook-form-chakra-ui-3nbwup
Steps to reproduce
I have react-hook-form with Chakra-ui and after updating to the latest version of
react-hook-form
(7.33.1) and@types/react
(18.0.15) and@types/react-dom
(18.0.6)When creating a component and using
useFormContext()
, when trying to display the error message inChakra-ui
<FormErrorMessage>
component I'm getting this error:Type 'Merge<FieldError, FieldErrorsImpl<DeepRequired>> | undefined' is not assignable to type 'ReactNode'.
Type 'Merge<FieldError, FieldErrorsImpl<DeepRequired>>' is not assignable to type 'ReactNode'.
Type 'Merge<FieldError, FieldErrorsImpl<DeepRequired>>' is not assignable to type 'string'.
This only happens when we use
useFormContext()
. As you can see in the provided codesandbox, when registering an input with{...register('lastName')}
I don't have this issue.Expected behaviour
Expected errors to not throws type errors.
What browsers are you seeing the problem on?
No response
Relevant log output
No response
Code of Conduct
The text was updated successfully, but these errors were encountered: