admin管理员组文章数量:1279018
I try using the hook useFormState
from react-hook-form
in a react-admin
form
I am a beginner for both modules so maybe I use them wrong but I searched through both of the documentations and I could not find something saying I was having a wrong use.
Here is my code
import { useFormState } from 'react-hook-form';
import {
Create,
Form,
TextInput,
} from 'react-admin';
const MyCustomComponent = () => {
const { errors, isDirty } = useFormState();
return (
<div>
<h3>State</h3>
<pre>{JSON.stringify({ errors, isDirty }, null, 2)}</pre>
</div>
);
};
export const CreateComponent = () => (
<Create>
<Form>
<TextInput source="title" />
<TextInput source="description" />
<MyCustomComponent />
</Form>
</Create>
);
I don't understand why but I keep getting the error Cannot read properties of null (reading 'control')
I read for other subjects that it meant my hook call wasn't in a FormProvider
but it seems pretty obvious that my custom component is inside a FormProvider
If I add those lines inside my component:
const context = useFormContext();
console.log(context);
context is null
I really don't understand and can't make it work Please could you tell me if I am fetting something ? Should I add default values since I am using typescript ?
I try using the hook useFormState
from react-hook-form
in a react-admin
form
I am a beginner for both modules so maybe I use them wrong but I searched through both of the documentations and I could not find something saying I was having a wrong use.
Here is my code
import { useFormState } from 'react-hook-form';
import {
Create,
Form,
TextInput,
} from 'react-admin';
const MyCustomComponent = () => {
const { errors, isDirty } = useFormState();
return (
<div>
<h3>State</h3>
<pre>{JSON.stringify({ errors, isDirty }, null, 2)}</pre>
</div>
);
};
export const CreateComponent = () => (
<Create>
<Form>
<TextInput source="title" />
<TextInput source="description" />
<MyCustomComponent />
</Form>
</Create>
);
I don't understand why but I keep getting the error Cannot read properties of null (reading 'control')
I read for other subjects that it meant my hook call wasn't in a FormProvider
but it seems pretty obvious that my custom component is inside a FormProvider
If I add those lines inside my component:
const context = useFormContext();
console.log(context);
context is null
I really don't understand and can't make it work Please could you tell me if I am fetting something ? Should I add default values since I am using typescript ?
Share Improve this question edited Feb 25 at 8:02 Paulo-99 asked Feb 24 at 17:50 Paulo-99Paulo-99 356 bronze badges1 Answer
Reset to default 0This issue could be due to having duplicate versions of react-hook-form
(one direct dependency in your project and one brought by react-admin).
You can run npm list react-hook-form
to check if you have duplicate versions.
Alternatively you can check your lockfile (package-lock.json
or yarn.lock
depending on your package manager).
To dedupe the package you can run npm dedupe react-hook-form
or yarn dedupe react-hook-form
.
Or, edit the lockfile manually.
Or, use yarn’s resolutions
or npm’s overrides
:
// in packages.json
{
// ...
"resolutions": {
"react-hook-form": "7.54.2"
}
}
本文标签: Using useFormState in a reactadmin Form seems brokenStack Overflow
版权声明:本文标题:Using useFormState in a react-admin Form seems broken - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741249789a2365576.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论