DEV Community

Cover image for You don't know useEffect

You don't know useEffect

Trung Hieu Nguyen on August 15, 2021

useEffect is one of the most common React Hooks which every ReactJS developer should know. But using useEffect the right way is not easy as you mig...
Collapse
 
ilikebuttons profile image
ilikebuttons

Well done! I've read a lot of articles about hooks but this is the first one that actually explains what side effects are.

Two minor corrections:
Memorization should be Memoization.
Your setInterval will log every second, not every minute.

Collapse
 
trunghieu99tt profile image
Trung Hieu Nguyen

Thank you very much. :D Fixed

Collapse
 
ponikar profile image
Ponikar

Hey there, It was amazing. I am having a little doubt.
Is it worth it to memoize the component who has useEffect hook?

Every time some dependencies is going to be changed, the callback function will trigger! So what's meaning of memoization here!

Collapse
 
trunghieu99tt profile image
Trung Hieu Nguyen • Edited

It depends on the scenario, the main purpose of using Memoization here is to prevent unnecessary re-render (i.e the List only cares about fakeList, but it's re-rendered just because of changes of other stuff). Actually in real-life project, determining whether to use Memoization or not is much more complex than that.

Collapse
 
ponikar profile image
Ponikar

That's great.

Collapse
 
haosik profile image
Nikolay Mochanov

Cool article. Thanks.
By the way, the 'useCallback+useMemo' sandbox example is a bit wrong. It includes console.log for mounted List while not having fakeList declaration wrapped into useMemo.

Collapse
 
apaatsio profile image
Antti Ahti

Nice article, thanks. There are a couple of errors in the first code snippet under section 2b, though.

  1. It's missing import for useEffect
  2. It should use object destructuring in the List component, i.e. const List = memo(({ list }) => ... (note the curly braces around list)
  3. fakeList should be an array fakeList = [...] and not a function fakeList = () => [...]
Collapse
 
tanth1993 profile image
tanth1993

great. when I start to learn useEffect at the beginning. I didn't know why all effects were called at the first time even though the dependancies didn't change. Now I know the tip from you. However, I had to arrange useEffect in order so that the effect work as I expected.

Collapse
 
srikanth597 profile image
srikanth597

It's been sometime I used react,
If my memory is correct, ur useRef variable isMounted should also be automatically part of dependencies array doesn't it?
If u don't add it, IDE doesn't complain?

Maybe someone could u get this to my head.

Collapse
 
haosik profile image
Nikolay Mochanov

If I understood your question right - no, ref (isMounted in this case) should not be a part of dependencies because it is something different (from the 'state' for example) by nature. That said, changing useRef.current value does not cause the re-render, so it's just being changed 'silently' and only help the useEffects to fire when they really should.

Collapse
 
avishkardalvi profile image
AvishkarDalvi

Great post, anyone who is confused about useEffecf must read this.

Collapse
 
rjitsu profile image
Rishav Jadon

Great post! Loved the bit about using refs. I discovered this at work and looked for a solution and found it.

Collapse
 
codeofrelevancy profile image
Code of Relevancy

Saved a lot time. I really likes isMounted solution, it works well for my app. Thank you for sharing..

Collapse
 
the_yamiteru profile image
Yamiteru

I believe useEffect shouldn't even exist. It's an anti-pattern, a bad practice. Instead we should be able to subscribe to changes of individual variables.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
trunghieu99tt profile image
Trung Hieu Nguyen

About the title, it's a way to build interest among readers.
In my opinion, all blog posts are retell the official docs someway, but the point here is that I try to explain and give examples about some cases I think it's not really clear in official doc and want to share my point of view and my experience about it. :D Sorry if it bothers you.

Collapse
 
iamgideonidoko profile image
Gideon Idoko

You are right @trunghieu99tt .

Collapse
 
datlechin profile image
Ngô Quốc Đạt

Hay quá anh

Collapse
 
imjituchauhan profile image
Jitu Chauhan

Awesome Thank you.

Collapse
 
webpig profile image
朱宝华

Good!

Collapse
 
gleisser profile image
Gleisser

amazing post, thanks bro!

Collapse
 
kurumeii profile image
Kurumeii

đẳng cấp là đây chứ đâu 😁👍
có điều đôi khi mình vẫn phải vô đây xem lại để làm cái reference vì thằng useEffect này kg quen sẽ bị bug ở đâu mà kg biết như nào XD

Collapse
 
elsyng profile image
Ellis

This is exactly the kind of article we miss on React.👍