Hydration error best practice
Unanswered
Siamese Crocodile posted this in #help-forum
Siamese CrocodileOP
Hi. I am using Dialog from radix ui thats open based on the search params. I am experiencing hydration errors when I enter url
I came up with two solutions but I am not sure which is better.
1. Create smth like useClient hook like in nextjs docs
2. Import login modal using
localhost:3000?modal=login directly in the browser and I guess its because of prerendering - the dialog on the server did not render and on the client when nextjs was hydrating it did render.I came up with two solutions but I am not sure which is better.
1. Create smth like useClient hook like in nextjs docs
const [isClient, setIsClient] = useState(false);
useEffect(() => {
setIsClient(true);
}, []);
return (
<Dialog
open={isClient && params.get('modal') === 'login'}2. Import login modal using
dynamic with ssr: false9 Replies
@Siamese Crocodile Hi. I am using Dialog from radix ui thats open based on the search params. I am experiencing hydration errors when I enter url `localhost:3000?modal=login` directly in the browser and I guess its because of prerendering - the dialog on the server did not render and on the client when nextjs was hydrating it did render.
I came up with two solutions but I am not sure which is better.
1. Create smth like useClient hook like in nextjs docs
ts
const [isClient, setIsClient] = useState(false);
useEffect(() => {
setIsClient(true);
}, []);
return (
<Dialog
open={isClient && params.get('modal') === 'login'}
2. Import login modal using `dynamic` with `ssr: false`
Radix dialog uses radix portal, which currently has a bug where it cannot be open during mount. Hence the hydration error. The recommended solution is similar to your (1) yes, basically you default open to false and set it to true in a useEffect
I guess i will stick to useEffect but I am not sure if it has any advantages over just skipping SSR
@Siamese Crocodile I guess i will stick to useEffect but I am not sure if it has any advantages over just skipping SSR
One advantage I can immediately think of is animation
Siamese CrocodileOP
Hm
I did not see any difference
when I tried both of these
Well… if it works it works, so…
If it works for you, I don’t see a reason why you would need to change