Skip to content

Conversation

@monochromer
Copy link

We use server side rendering and useLayoutEffect spam into our logs with message:

useLayoutEffect does nothing on the server, because its effect cannot be encoded into the server renderer's output format. This will lead to a mismatch between the initial, non-hydrated UI and the intended UI. To avoid this, useLayoutEffect should only be used in components that render exclusively on the client. See https://reactjs.org/link/uselayouteffect-ssr for common fixes.

@gravity-ui
Copy link
Contributor

gravity-ui bot commented Nov 17, 2025

🎭 Component Tests Report is ready.

Comment on lines 3 to 4
export const useIsomorphicLayoutEffect =
typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export const useIsomorphicLayoutEffect =
typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
export const useLayoutEffect = typeof window === 'undefined' ? () => {} : React.useLayoutEffect;

Using the name useLayoutEffect allows to lint the hook's dependencies. There is also no need to call useEffect as it has no effect on the server.

const prevRootClassName = React.useRef('');

React.useLayoutEffect(() => {
useIsomorphicLayoutEffect(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other places where useLayoutEffect is used.

@monochromer monochromer requested a review from ValeraS November 18, 2025 09:47
@monochromer monochromer changed the title fix(ThemeProvider): add useIsomorphicLayoutEffect to fix SSR warnings fix: add useIsomorphicLayoutEffect to fix SSR warnings Nov 18, 2025

import * as React from 'react';

import {useLayoutEffect} from 'src/hooks/private';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import {useLayoutEffect} from 'src/hooks/private';
import {useLayoutEffect} from '../../hooks/private';

path should be relative

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants