MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Synchronous Layout Calculations with useLayoutEffect0%
Study Materials
React.js for AdvancedProgress 0%
1.1Control Props and Inversion of Control Pattern1.2State Reducer Pattern in Advanced React1.3Prop Getters and Flexible Design Systems1.4Headless UI Architecture and Accessibility Primitives
7.1Concurrent Rendering with useTransition7.2Deferred Value Updates with useDeferredValue7.3Synchronous Layout Calculations with useLayoutEffect7.4External Store Synchronization with useSyncExternalStore
HomeTutorialsReact.js for AdvancedSynchronous Layout Calculations with useLayoutEffect
External Store Synchronization with useSyncExternalStore

Synchronous Layout Calculations with useLayoutEffect

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

React's useEffect is asynchronous and non-blocking: it executes after the browser paints the updated DOM to the screen. While this preserves 60 FPS UI performance for 95% of side effects, it causes visible visual flicker when measuring DOM nodes (e.g., tooltips, popovers, dropdown positions) and updating state before painting. useLayoutEffect runs synchronously before the browser paints.


1. The Rendering Lifecycle: useEffect vs useLayoutEffect

1React Component Updates
2 │
3 ▼
4React reconciles Virtual DOM and mutates physical DOM nodes
5 │
6 ├───> useLayoutEffect fires SYNCHRONOUSLY
7 │ (Can inspect layout, measure dimensions, mutate DOM/state)
8 │
9 ▼
10Browser paints pixels to screen (layout and paint)
11 │
12 ├───> useEffect fires ASYNCHRONOUSLY
13 (Data fetching, subscriptions, analytics)

If you position a tooltip dynamically inside useEffect:

  1. 1
    The DOM mounts with tooltip at (0, 0).
  2. 2
    The browser paints: user briefly sees the tooltip flicker at the top-left corner of the screen for 1 frame.
  3. 3
    useEffect runs, measures the trigger button, and sets coordinates (250, 180).
  4. 4
    The browser re-paints: tooltip jumps to the correct position.

With useLayoutEffect, the coordinate calculation happens before step 2, preventing any visual jump.


2. Dynamic Tooltip Positioning Example

1import React, { useState, useRef, useLayoutEffect } from "react";
2 
3interface Position {
4 top: number;
5 left: number;
6}
7 
8export function FloatingTooltipButton() {
9 const [showTooltip, setShowTooltip] = useState(false);
10 const [coords, setCoords] = useState<Position>({ top: 0, left: 0 });
11 
12 const buttonRef = useRef<HTMLButtonElement>(null);
13 const tooltipRef = useRef<HTMLDivElement>(null);
14 
15 // useLayoutEffect guarantees coordinates are calculated BEFORE browser paint
16 useLayoutEffect(() => {
17 if (!showTooltip || !buttonRef.current || !tooltipRef.current) return;
18 
19 const buttonRect = buttonRef.current.getBoundingClientRect();
20 const tooltipRect = tooltipRef.current.getBoundingClientRect();
21 
22 // Position tooltip centered directly above the trigger button
23 const calculatedTop = buttonRect.top - tooltipRect.height - 8;
24 const calculatedLeft = buttonRect.left + (buttonRect.width / 2) - (tooltipRect.width / 2);
25 
26 setCoords({
27 top: Math.max(8, calculatedTop),
28 left: Math.max(8, calculatedLeft),
29 });
30 }, [showTooltip]);
31 
32 return (
33 <div className="p-20 bg-slate-900 min-h-screen text-slate-100 flex items-center justify-center">
34 <button
35 ref={buttonRef}
36 onMouseEnter={() => setShowTooltip(true)}
37 onMouseLeave={() => setShowTooltip(false)}
38 className="px-6 py-3 bg-cyan-600 hover:bg-cyan-500 rounded-lg font-semibold shadow-lg transition"
39 >
40 Hover For Tooltip
41 </button>
42 
43 {showTooltip && (
44 <div
45 ref={tooltipRef}
46 style={{
47 position: "fixed",
48 top: `${coords.top}px`,
49 left: `${coords.left}px`,
50 }}
51 className="z-50 px-3 py-1.5 bg-slate-800 border border-slate-700 text-cyan-300 text-xs rounded shadow-2xl pointer-events-none"
52 >
53 Dynamic tooltip anchored seamlessly without flicker!
54 </div>
55 )}
56 </div>
57 );
58}

3. Server-Side Rendering (SSR) Warning

Because useLayoutEffect requires direct access to DOM geometric layout engines (getBoundingClientRect, offsetHeight), calling it during Server-Side Rendering (SSR in Next.js or Node.js) generates a warning:

Important Warning
"Warning: useLayoutEffect does nothing on the server, because its effect cannot be encoded into the server renderer's output format."

Isomorphic Layout Effect Solution

To silence this warning in universal codebases, conditionally alias useLayoutEffect to useEffect on the server:

1import { useEffect, useLayoutEffect } from "react";
2 
3export const useIsomorphicLayoutEffect =
4 typeof window !== "undefined" ? useLayoutEffect : useEffect;

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
When does useLayoutEffect execute in the React component lifecycle?
Question 2
Why does dynamic positioning inside standard useEffect cause visual flicker?
Question 3
What is the risk of performing heavy computational loops inside useLayoutEffect?
Question 4
Why does useLayoutEffect trigger a console warning during Server-Side Rendering (SSR)?
Question 5
What is the purpose of the useIsomorphicLayoutEffect pattern?
Next Lesson

External Store Synchronization with useSyncExternalStore

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Deferred Value Updates with useDeferredValueExternal Store Synchronization with useSyncExternalStore

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext