ReactAdvanced#hooks#effects

What is the difference between useEffect and useLayoutEffect?

useEffect runs asynchronously after paint, so it never blocks visual updates. useLayoutEffect runs synchronously after DOM mutation but before paint, which is required for measuring layout to avoid flicker.

Example
useLayoutEffect(() => {
  const h = ref.current.getBoundingClientRect().height;
  setHeight(h); // measured before the user sees it
}, []);

Related Questions

1
ReactIntermediate#effects#debugging

What is StrictMode and why does my effect run twice?

Open
2
ReactAdvanced#state#patterns

How do you share state between distant components?

Open
3
ReactAdvanced#state#data

What is the difference between server state and client state?

Open