ZachSelindh
ZachSelindh

Reputation: 61

FormContext returns 'null' inside SimpleForm on local

Hoping someone can point me in the right direction to try to debug this issue.

I'm refactoring an application built with react-admin to version 4.0.0.beta-3. But none of my custom forms are working correctly. Namely, when I call any of the new react-hook-form methods, like "useFormContext", I get back null. When I try to get the form values using "useWatch", I get back an error saying 'Cannot read properties of null (reading 'control')'

According to react-hook-form's docs, this may indicate that the element is not being provided with FormContext. However, when I built a codesandbox to verify the issue, useWatch works correctly and returns the form values. (Here is that codesandbox: https://codesandbox.io/s/crazy-violet-qk7uyh?file=/src/test-resource/TestResourceCreate.tsx )

If I copy the code nearly line-for-line into my local environment, it breaks, looking for "control" when I call useWatch.

I've tried deleting node_modules and reinstalling, I've installed react-hook-form at 7.27.0 (the stated version react-admin is using), but I have not found a solution.

Any suggestions for what to do/try next, or what could be wrong with my local, would be very much appreciated. Thanks!

EDIT: As an addendum: When I use the react-admin form component "FormDataConsumer", the form values are correctly passed in the prop "formData".

Upvotes: 2

Views: 1815

Answers (1)

ZachSelindh
ZachSelindh

Reputation: 61

Answering my own question for posterity:

After refactoring my application to avoid having to flag --legacy-peer-deps on running "npm install", I was able to use useFormContext and all other react-hook-form methods as expected inside my react-admin custom forms.

The only package I'm aware of that required a legacy peer dependency was installing React at 16.9.0 while the rest of my application relied on 17.0.2. I'm unsure if that was source of my issue or if some other legacy peer was being installed that resulted in an incorrect import somewhere else. If anyone with more experience wants to share some insight into what could have been occurring to as to helper future searchers, be my guest.

Upvotes: 1

Related Questions