Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Hooks: What is the difference between 'useMutationEffect' and 'useLayoutEffect'?

What is the difference between useMutationEffect and useLayoutEffect in term of usage?

I have read all available material online including (but not limited to)

  1. Hooks Reference
  2. Blog post by Kent

Difference between useEffect and other two hooks is clear. but difference between useMutationEffect and useLayoutEffect is still not clear.

I know the order of execution is:

  1. useMutationEffect
  2. useLayoutEffect
  3. useEffect
like image 763
Abdul Rauf Avatar asked Nov 28 '18 07:11

Abdul Rauf


People also ask

What is the difference between useEffect () and useLayoutEffect ()?

useLayoutEffect is identical to useEffect, but it's major key difference is that it gets triggered synchronously after all DOM mutation. You only want to use this hook when you need to do any DOM changes directly.

What is useLayoutEffect hook in React?

useLayoutEffect. The signature is identical to useEffect , but it fires synchronously after all DOM mutations. Use this to read layout from the DOM and synchronously re-render. Updates scheduled inside useLayoutEffect will be flushed synchronously, before the browser has a chance to paint.

Why is useLayoutEffect used?

The useLayoutEffect function is triggered synchronously before the DOM mutations are painted. However, the useEffect function is called after the DOM mutations are painted. I chose this example to make sure the browser actually has some changes to paint when the button is clicked, hence the animation.

When would you want to avoid useEffect and use useLayoutEffect instead?

One special case. One other situation you might want to use useLayoutEffect instead of useEffect is if you're updating a value (like a ref ) and you want to make sure it's up-to-date before any other code runs. For example: const ref = React.


1 Answers

First, you have to understand the different phases of Rendering.

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. We should use either useMutationEffect or useLayoutEffect for this specific case to perform blocking visual updates. The only difference between these two is we should use useLayoutEffect if we want to read Layout from the DOM. Otherwise, we should use useMutationEffect.

  1. useMutationEffect

It fires synchronously before Layout phase i.e. during the same phase that React performs its DOM mutations. Use it to perform blocking custom DOM mutations without taking any DOM measurement/reading layout.

  1. useLayoutEffect

It fires synchronously after all DOM mutations but before Paint phase. Use this to read layout(styles or layout information) from the DOM and then perform blocking custom DOM mutations based on layout.

  1. useEffect

It runs after the render is committed to the screen i.e. after Layout and Paint phase. Use this whenever possible to avoid blocking visual updates.

Update: useMutationEffect hook has been removed from Hooks API in this PR. (Credits: Dhaval Patel)

like image 88
Abdul Rauf Avatar answered Sep 19 '22 12:09

Abdul Rauf