next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Using new `useInsertionEffect`.

Open sanjaiyan-dev opened this issue 2 years ago • 2 comments

Hi, I thought to use new useInsertionEffect for <Head /> component hence it used to toggle CSS efficiently where previously we used useLayoutEffect.

// Dummy component that we render as a child of Root so that we can
// toggle the correct styles before the page is rendered.
function Head({ callback }: { callback: () => void }): null {
  // We use `useInsertionEffect` to guarantee the callback is executed
  // as soon as React flushes the update.
  React.useInsertionEffect?.(() => callback(), [callback])
  return null
}

Sorry if I made any mistakes :(

sanjaiyan-dev avatar Aug 03 '22 03:08 sanjaiyan-dev

Hi, I thought to use new useInsertionEffect for <Head /> component hence it used to toggle CSS efficiently where previously we used useLayoutEffect.

// Dummy component that we render as a child of Root so that we can
// toggle the correct styles before the page is rendered.
function Head({ callback }: { callback: () => void }): null {
  // We use `useInsertionEffect` to guarantee the callback is executed
  // as soon as React flushes the update.
  React.useInsertionEffect?.(() => callback(), [callback])
  return null
}

Sorry if I made any mistakes :(

useInsertionEffect should only be used in the CSS-in-JS libraries, as the insertion effect commits before Virtual DOM is committed to the "real" DOM, thus preventing the "flash of the screen".

And IMHO <Head /> shouldn't be used to toggle the CSS (The existing codebase should switch to useInsertionEffect instead of relying on <Head />), thus the useInsertionEffect doesn't fit here.

The common usage of <Head /> would be updating title, OpenGraph, and other meta tags (which useEffect should be used instead).

SukkaW avatar Aug 03 '22 14:08 SukkaW

Hi, I thought to use new useInsertionEffect for <Head /> component hence it used to toggle CSS efficiently where previously we used useLayoutEffect.

// Dummy component that we render as a child of Root so that we can
// toggle the correct styles before the page is rendered.
function Head({ callback }: { callback: () => void }): null {
  // We use `useInsertionEffect` to guarantee the callback is executed
  // as soon as React flushes the update.
  React.useInsertionEffect?.(() => callback(), [callback])
  return null
}

Sorry if I made any mistakes :(

useInsertionEffect should only be used in the CSS-in-JS libraries, as the insertion effect commits before Virtual DOM is committed to the "real" DOM, thus preventing the "flash of the screen".

And IMHO <Head /> shouldn't be used to toggle the CSS (The existing codebase should switch to useInsertionEffect instead of relying on <Head />), thus the useInsertionEffect doesn't fit here.

The common usage of <Head /> would be updating title, OpenGraph, and other meta tags (which useEffect should be used instead).

Sorry, I thought this component used to toggle only CSS as it was described in comments 🤔

sanjaiyan-dev avatar Aug 05 '22 09:08 sanjaiyan-dev

https://github.com/vercel/next.js/pull/53617#issuecomment-1666610953

leerob avatar Aug 16 '23 14:08 leerob