Open raleigh9123 opened 1 year ago
@raleigh9123 I've the same issue in a next.js app caused by DevTools. I use the following dynamic import workaround for now:
const DevT = dynamic(
() =>
import('@hookform/devtools').then((module) => {
return module.DevTool
}),
{ ssr: false },
)
@Nurou 's answer worked for me! Thank you!!
Here's a slightly updated, typescript-compatible version of it that worked for me (hope it can save a few minutes for others like me who enjoy having a painful eslint config 😄 ):
import dynamic from 'next/dynamic';
const DevT: React.ElementType = dynamic(
() => import('@hookform/devtools').then((module) => module.DevTool),
{ ssr: false }
);
Then, (like @Nurou insinuates), I can use it like so:
...
return (
<FormProvider {...methods}>
<DevT control={methods.control} placement="top-left" />
...
</FormProvider>
)
@adamwdennis cool.. working..
thank you so much
React.ComponentType
is the most appropriate type. It is more specific than React.ElementType
and represents a React component class or a function component, which is what dynamic
is expected to return.
Here is the updated code:
const DevTool: React.ComponentType = dynamic(
() => import("@hookform/devtools").then((module) => module.DevTool),
{ ssr: false },
);
<DevTool control={control} />
Update: This will not work. VSCode will still complain about the control
attribute:
Type '{ control: Control<Inputs, any>; }' is not assignable to type 'IntrinsicAttributes'.
Property 'control' does not exist on type 'IntrinsicAttributes'.ts(2322)
(property) control: Control<Inputs, any>
So go with @adamwdennis' answer here: https://github.com/react-hook-form/devtools/issues/187#issuecomment-1369182795
const DevTool: React.ElementType = dynamic(
() => import("@hookform/devtools").then((module) => module.DevTool),
{ ssr: false },
);
<DevTool control={control} />
Update 2:
Or you could do the following - seems to work too:
"use client";
import dynamic from "next/dynamic";
import { SubmitHandler, useForm, Control } from "react-hook-form";
type Inputs = {
firstName: string;
};
interface DevToolProps {
control: Control<Inputs>;
}
const DevTool: React.ComponentType<DevToolProps> = dynamic(
() => import("@hookform/devtools").then((module) => module.DevTool),
{ ssr: false },
);
export default function Step01() {
const { register, handleSubmit, control } = useForm<Inputs>({
mode: "onChange",
});
const onSubmit: SubmitHandler<Inputs> = (data) => console.log(data);
return (
<>
<form onSubmit={handleSubmit(onSubmit)}>
<input
className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
defaultValue="Homer"
{...register("firstName")}
/>
<input
className="rounded-md bg-indigo-600 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
type="submit"
/>
</form>
<DevTool control={control} />
</>
);
}
I thought I had done something wonky in my app when these errors started appearing. I rolled back on some changes and learned that they appeared when I refresh the page that has the component imported.
I am working within a Blitz.js app which is built on Next.js (I am unsure, but am lead to believe that this is a problem within Next.js?)
The
component used by default in the Blitz.js installation is a little bit of a beast, in my opinion, and I added a couple more details to allow me to pass a custom prop to the child inputs.My Form Component
Again, the hydration errors ONLY occur when I refresh the page. They are listed in the following screenshot. I have not tried anything as a solution, but they do not appear when I remove the component.
The following is my package.json dependencies: