-
Notifications
You must be signed in to change notification settings - Fork 57
Description
Describe the issue
A useIsomorphicLayoutEffect hook was added in PR #149.
This hook is designed to bypass the warning that occurs when using useLayoutEffect on the server, and instead run useLayoutEffect when the code is executed in the browser.
However, there's an important point that needs to be addressed here. If you look at the implementation of useIsomorphicLayoutEffect, you'll see that it attempts to use useEffect in server environments.
But React does not execute useEffect on the server at all — meaning that the fallback to useEffect is entirely unnecessary.
The core issue here is that the name isomorphic may give users the impression that the hook behaves identically across both the server and the client. In reality, this is not the case. The actual purpose of this hook is simply to suppress warnings during SSR (Server-Side Rendering).
Frankly, I would go as far as to call this kind of hook an anti-pattern.
Using useLayoutEffect in server environments can cause problems, and React issues a warning specifically to prevent this.
However, the name useIsomorphicLayoutEffect may mislead users into thinking it allows them to use useLayoutEffect without warning — when in reality, it's just being run in the browser, as usual.
Because of this, there is a growing sentiment in favor of using a more explicit hook like the following:
import { useLayoutEffect } from "react";
export function useClientLayoutEffect(
...args: Parameters<typeof useLayoutEffect>
) {
if (typeof document === "undefined") return;
useLayoutEffect(...args);
}The code may require some adjustments in detail, but I trust the intent is clear.
I propose deprecating useIsomorphicLayoutEffect and introducing useClientLayoutEffect instead.
Additional context
The issue described here — the problems caused by useIsomorphicLayoutEffect — is explained in great detail in the article linked below.
If you're interested, I highly recommend giving it a read.
https://smoores.dev/post/no_such_thing_isomorphic_layout_effect/