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

NextJS static build throws error unless errors object passed to ErrorMessage #91

Open
bennettrogers opened this issue Mar 11, 2022 · 7 comments
Labels
bug Something isn't working

Comments

@bennettrogers
Copy link

I've been using ErrorMessage with no issues on NextJS client-rendered pages. Based on the documentation, I haven't been passing the errors object to the ErrorMessage component since I'm using a FormProvider.

However, I just tried to use an ErrorMessage in a form on a NextJS statically exported page and it throws an error that says TypeError: Cannot read properties of null (reading 'formState').

After looking through the issues here and reading #52, I tried fetching the errors object inside my component using useFormContext, and passing this to the ErrorMessage. It seems to have resolved the issue.

This workaround works for me, but I wanted to see if this is expected behavior when exporting a static page.

Versions:
NextJS v12.1.0
React-Hook-Form v7.27.1
@hookform/error-message v2.0.0

@IngoValente
Copy link

Same here. Somehow this error was not triggered using next 11, but broke as soon as we upgraded to next 12.
Error only occurs when server renders the page.

Simple replication sandbox here: https://codesandbox.io/s/cocky-hellman-toj9b9?file=/pages/index.js
To replicate: remove the passed errors from ErrorMessage, and hit the refresh button in the sandbox browser.
Passing the errors or downgrading to next 11 solves the issue.

@lennerd
Copy link

lennerd commented Aug 4, 2022

I can confirm this. Both with a Remix and vitest setup.

@bluebill1049 bluebill1049 added the bug Something isn't working label Nov 9, 2022
@dextermb
Copy link

dextermb commented Dec 9, 2022

I've just come across this issue as well.

{
    "@hookform/error-message": "^2.0.1",
    "next": "13.0.6",
    "react-hook-form": "^7.40.0"
}

@fknop
Copy link

fknop commented Mar 12, 2023

Same error with an NX workspace setup using webpack. It seems this library doesn't import the same version of the context

@elramus
Copy link

elramus commented Jun 13, 2023

Anyone found a workaround for this? Running into it with Vitest. We can just mock the component of course, but it would be nice to be able to include its functionality in our unit testing.

@KirschX
Copy link

KirschX commented Oct 31, 2023

Same error here, passing errors directly from the parent component solved the error.

...
  const methods = useFormContext();
  const error = get(errors || methods.formState.errors, name);
...

seems like methods from useFormContext returns 'undefined', still don't know why.

@rtkac
Copy link

rtkac commented Nov 15, 2023

Same issue here after update to NX v15.9.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants