Skip to content

Note with examples about the use cases of the tools in react and how to use it to solve practical problems.

Notifications You must be signed in to change notification settings

turbo8p/react-usecase-note

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

Note with examples to illustrate the use cases of functions, hooks in react.

This repo also includes the common problems when working with react and how to solve those problems.

When to use useEffect() and useLayoutEffect()

Statement about useEffect() from React documentation:

However, not all effects can be deferred. For example, 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.

For these types of effects, React provides one additional Hook called useLayoutEffect

This section shows you the example that will illustrate exactly what the above statement means.

Visual inconsistency

2021-11-12_7-44-06

Explaination:

Also check the code here.

With useEffect(), the effect of making 🟢 color is defered after paint. So browser will paint the 🔴 color first and then the effect takes place afterward.

With useLayoutEffect() the effect of making 🟢 color is not deferred. Before the browser has a chance to paint, the effect is taken place already.

** If the effect you put in the useLayoutEffect() is slow and take long time to process, this will result in UI blocking (not render until your effect computation is done). So you need to evaluate your problem before implemeting the solution.

About

Note with examples about the use cases of the tools in react and how to use it to solve practical problems.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published