Skip to content

Instantly share code, notes, and snippets.

@lwkchan
Last active August 22, 2021 16:03
Show Gist options
  • Save lwkchan/dd272155fead41f9931f7b4a1799c68c to your computer and use it in GitHub Desktop.
Save lwkchan/dd272155fead41f9931f7b4a1799c68c to your computer and use it in GitHub Desktop.

useLayoutEffect vs useEffect

useEffect

  • Fires after render (to virtual DOM) and browser paint
  • Will not block the browser from updating the screen as it runs asynchronously
  • Useful for side effects which are not concerned with DOM information

useLayoutEffect

  • Similar to componentDidMount/componentDidUpdate
  • Runs synchronously run after the DOM has been modified
  • For effects when, e.g. 'a DOM mutation that is visible to the user must fire synchronously before the next paint so that the user does not perceive a visual inconsistency'
  • Useful for when you need to read the size or position of a DOM node before the next browser paint

Resources

@yairEO
Copy link

yairEO commented May 11, 2020

Runs synchronously run

wrong grammar :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment