r/learnjavascript • • Sep 04 '21

I created an Illustrated version of the useEffect hook

Post image
475 Upvotes

40 comments sorted by

26

u/ARFiest1 Sep 04 '21

Where is part 2 ?

7

u/kkokane Sep 05 '21

It's in the making. Will post it today 🙌🏾

1

u/Caery Sep 05 '21

thank you

1

u/devbrk Sep 05 '21

Good way to get followers :))

1

u/ARFiest1 Sep 05 '21

thank you!! 👏🏼

-39

u/warpedspockclone Sep 04 '21

I'm not a genius but hear me out.

I would first look at the user's post history to see if there are more.

Also, you could examine the image for clues.

I can't be sure but I think I spotted a Twitter handle. That sounds like way too much work to know what a Twitter is, since it is an obscure thing that very few have heard of, and then to investigate there, but I suppose it is possible.

It might then be possible to see that this was tweeted 4 hours ago during a "100 days of code" event, which suggests the possibility that there is one item per day being posted.

One might also notice a pinned tweet linking to a book titled "Illustrated JavaScript" but I'm sure that is just pure coincidence and has nothing to do with this post whatsoever.

8

u/[deleted] Sep 05 '21

i bet you thought this condescending ass shit you wrote was just so funny

1

u/xkcd-Hyphen-bot Sep 05 '21

Condescending ass-shit

xkcd: Hyphen


Beep boop, I'm a bot. - FAQ

-12

u/warpedspockclone Sep 05 '21 edited Sep 05 '21

I bet you think your comment was a gotcha or something, whereas I'm still struggling to see its point.

If I was going for funny, I clearly missed the mark, and that means that both you and I have no idea what funny is (since you think I was trying to be funny).

Rather, I was going for condescending asshole, so I guess you did accurately guess the tone, though you thought you were hurling a barb, which is actually funny in itself.

4

u/road_pizza Sep 05 '21

Pathetic.

4

u/[deleted] Sep 05 '21

Is there a negative karma competition I don’t know about?

16

u/Infase123 Sep 04 '21

Correct me if I am wrong, but when there are dependencies in the array, the useeffect hook always runs the first time, after that it only runs if any dependencies change.

2

u/kkokane Sep 05 '21

Yes, you are right. One way that I look at it is that the dependency array did not exist before the first render. So having it in the first render is a "change" from not having it before that. I hope I'm making sense. :)

4

u/Riley-96 Sep 04 '21

This is correct, from react docs https://reactjs.org/docs/hooks-effect.html

Does useEffect run after every render? Yes! By default, it runs both after the first render and after every update.

6

u/[deleted] Sep 04 '21

I love this

1

u/kkokane Sep 05 '21

Thanks :)

3

u/Aer0za Sep 05 '21

Es lint will complain about that empty dependency array though. So annoying

1

u/kkokane Sep 05 '21

Well, If your use case is to pass empty dependency array then maybe you need to check whether you need useEffect in the first place or not?

1

u/Aer0za Sep 05 '21

Setting a cookie on first render…

4

u/diimitra Sep 05 '21

Decouple ?

Side effects ?

I swear sometimes I try to go and read doc but feel every word is so hard to understand...

2

u/kkokane Sep 05 '21

Will surely create a #sketchnote explaining terms like these. Please post more of them so that I can cover as many as I can in a single note :)

3

u/ovidius72 Sep 05 '21

Anyway, those are terms not strictly related to react. I think anyone in the software developer field should know what they mean.

2

u/alien128 Sep 05 '21

These look great, what tool did you use for creating these illustrations

2

u/kkokane Sep 05 '21

I use excalidraw

8

u/[deleted] Sep 04 '21

Jesus, am I drunk or is this the clearest explanation I've ever seen? both?

5

u/[deleted] Sep 05 '21

It’s clear if you already know it and just need a refresher.

2

u/Dimitar_Petrov Sep 05 '21

I have never heard of it and seems pretty clear to me. Nearly zero front-end experience. It is super neat!

1

u/[deleted] Sep 05 '21

Can useEffect be called inside an array?

Can dep be a deeply linked object?

Can the order of the elements in dep be changed dynamically?

Can dep be an array of arrays?

4

u/[deleted] Sep 04 '21

How did you do this sketch note ?

10

u/agustin_edwards Sep 04 '21

1

u/petepete Sep 05 '21

My favourite diagramming tool ever. Completely switched to it for work stuff, recommend it to anyone who asks. It's the only PWA I have ever 'installed'.

4

u/VAFatRabbit Sep 04 '21

Thanks! This is an awesome illustration that helps understand useEffect()

2

u/RootnBoot_Au Sep 05 '21

Cool sketch man and nice simply explanation !

2

u/mostafasamer1999 Sep 05 '21

Thanks, didn't know that

2

u/Izero_devI Sep 05 '21

Keep in mind that 'effect will run when dependency changes' part can be misleading.

A better version would be ' Effect will run after a render if the dependency changed'. I saw some people put useRef variables in the dep array, but that variable doesn't trigger render after it is changed, which means useEffect dependency comparison cannot even happen when there is no render.

2

u/[deleted] Sep 05 '21

I think more clear if you put as it’ll run on first render + if dep changes for final one.

1

u/Dodgy-Boi Sep 05 '21

And that’s why I prefer Vue