Skip to content

Commit 12655d8

Browse files
committed
Add useLayoutEffect questions
1 parent efb9974 commit 12655d8

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7010,6 +7010,78 @@ Technically it is possible to write nested function components but it is not sug
70107010
```
70117011
**[⬆ Back to Top](#table-of-contents)**
70127012
7013+
291. ### What are the usecases of useLayoutEffect?
7014+
You need to use `useLayoutEffect` when your effect **must run before the browser paints**, such as:
7015+
7016+
* **Reading layout measurements** (e.g., element size, scroll position)
7017+
* **Synchronously applying DOM styles** to prevent visual flicker
7018+
* **Animating layout or transitions**
7019+
* **Integrating with third-party libraries** that require DOM manipulation
7020+
7021+
If there's no visual or layout dependency, prefer `useEffect` — it's more performance-friendly.
7022+
7023+
```js
7024+
useLayoutEffect(() => {
7025+
const width = divRef.current.offsetWidth;
7026+
if (width < 400) {
7027+
divRef.current.style.background = 'blue'; // prevents flicker
7028+
}
7029+
}, []);
7030+
```
7031+
7032+
**[⬆ Back to Top](#table-of-contents)**
7033+
7034+
292. ### How does useLayoutEffect work during server-side rendering (SSR)?
7035+
7036+
The `useLayoutEffect` hook does **not run on the server**, because there is no DOM. React issues a warning in server environments like Next.js if `useLayoutEffect` is used directly.
7037+
7038+
This can be mitigated using a conditional polyfill:
7039+
7040+
```jsx
7041+
const useIsomorphicLayoutEffect =
7042+
typeof window !== 'undefined' ? useLayoutEffect : useEffect;
7043+
```
7044+
7045+
i.e, Use `useIsomorphicLayoutEffect` in components that render both on client and server.
7046+
7047+
**[⬆ Back to Top](#table-of-contents)**
7048+
7049+
293. ### What happens if you use useLayoutEffect for non-layout logic?
7050+
Using `useLayoutEffect` for logic **unrelated to layout or visual DOM changes** (such as logging, data fetching, or analytics) is **not recommended**. It can lead to **performance issues** or even unexpected behavior.
7051+
7052+
**Example: Anti-pattern**
7053+
```js
7054+
useLayoutEffect(() => {
7055+
console.log("Tracking analytics");
7056+
fetch('/log-page-view');
7057+
}, []);
7058+
```
7059+
The above usage delays the paint of the UI just to send a network request, which could (and should) be done after paint using useEffect.
7060+
7061+
**[⬆ Back to Top](#table-of-contents)**
7062+
7063+
294. ### How does useLayoutEffect cause layout thrashing?
7064+
The `useLayoutEffect` can **cause layout thrashing** when you **repeatedly read and write to the DOM** in ways that force the browser to recalculate layout multiple times per frame. This is because `useLayoutEffect` runs _before the browser paints_, these reflows happen _synchronously_, blocking rendering and degrading performance.
7065+
7066+
**Example:**
7067+
```js
7068+
function ThrashingComponent() {
7069+
const ref = useRef();
7070+
7071+
useLayoutEffect(() => {
7072+
const height = ref.current.offsetHeight; //Read
7073+
ref.current.style.height = height + 20 + 'px'; //Write
7074+
const newHeight = ref.current.offsetHeight; //Read again — forces reflow
7075+
}, []);
7076+
7077+
return <div ref={ref}>Hello</div>;
7078+
}
7079+
```
7080+
In the above code, each read/write cycle triggers synchronous reflows, blocking the main thread and delays UI rendering.
7081+
7082+
This issue can be avoided by batching your DOM reads and writes and prevent unnecessary reads after writes.
7083+
7084+
**[⬆ Back to Top](#table-of-contents)**
70137085
## Old Q&A
70147086
70157087
1. ### Why should we not update the state directly?

0 commit comments

Comments
 (0)