HomeGorakh Raj Joshi

useLayoutEffect

Both of these can be used to do basically the same thing, but they have slightly different use cases.

  • #Extra
import { useEffect, useLayoutEffect, useState } from 'react';

const Demo = () => {
  const [isNew, setIsNew] = useState(false);

  for (let i = 0; i < 10000; i++) {
    console.log('Slow down!');
  }

  // useEffect(() => {
  //   setIsNew(true)
  // }, [])

  useLayoutEffect(() => {
    setIsNew(true);
  }, []);

  return (
    <div>
      <p style={{ fontSize: '100px' }}>Admin: {isNew ? 'New Content' : 'Old Content'}</p>
    </div>
  );
};

export default Demo;

Gorakh Raj Joshi

Senior Fullstack Engineer: Specializing in System Design and Architecture, Accessibility, and Frontend Interface Design

LinkedIn

GitHub

Email

All rights reserved © Gorakh Raj Joshi 2024