question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

issue: Error message have wrong type when using useFormContext

See original GitHub issue

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 in Chakra-ui <FormErrorMessage> component I’m getting this error:

Type ‘Merge<FieldError, FieldErrorsImpl<DeepRequired<any>>> | undefined’ is not assignable to type ‘ReactNode’. Type ‘Merge<FieldError, FieldErrorsImpl<DeepRequired<any>>>’ is not assignable to type ‘ReactNode’. Type ‘Merge<FieldError, FieldErrorsImpl<DeepRequired<any>>>’ 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

  • I agree to follow this project’s Code of Conduct

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

8reactions
alvesheliocommented, Jul 9, 2022

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 FormValues to useFormContext<FormValues>() then it solves the problem. I’m just wondering how I can make it work when it comes to a component with a generic type. If I do something like this

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>
  );
};

Then I get this: image

3reactions
bluebill1049commented, Jul 9, 2022

Thanks for understanding @alveshelio I have updated the changelog to point this out, I am pretty excited for the next feature for useFieldArray rules prop, we can finally validate field array by the build-in validator. Make sure you cast your API preference in the poll if you are going to use it. https://github.com/react-hook-form/react-hook-form/pull/8562 Also thank you very much for the donation (forgot to mention that)

Read more comments on GitHub >

github_iconTop Results From Across the Web

useFormState - ErrorMessage - React Hook Form
A simple component to render associated input's error message. npm install @hookform/error-message. Props. Name, Type, Required, Description ...
Read more >
useForm - setError - React Hook Form
Set an error with its type and message. ... An error that is not associated with an input field will be persisted until...
Read more >
useForm - ClearErrors - React Hook Form
This function can manually clear errors in the form. Props. Type, Description, Example ... string : reset the error on a single field...
Read more >
Advanced Usage - React Hook Form
Error Messages. Error messages are visual feedback to our users when there are issues with their inputs. React Hook Form provides an errors...
Read more >
useForm | React Hook Form - Simple React forms validation
Only works with onSubmit and onChange mode due to reportValidity execution will focus on the error input. Each registered field's validation message is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found