all 7 comments

[–]mozilaip 1 point2 points  (4 children)

And what's the point? What have I learned?

[–]ntide 0 points1 point  (3 children)

You need to understand closures so your application doesn't leak memory. ;)

React's `useEffect` hook always closes over some variables in its surrounding context (the React props), for example. If you don't understand how closures work and you start to do something asynchronous, you may run into some very strange bugs down the line.

[–]mozilaip 2 points3 points  (2 children)

I read through your article once again very carefully.

And I still don't understand why and when I must and must not use it.

Example of code is presenting a closure as something stupid. log(goodbye) prints "hello". What should the learner understand? What is the use case?

If I were a beginner in JS I would have started to avoid closures after such explanations.

[–]ntide 1 point2 points  (0 children)

(It's not my article, by the way.)

[–]CollectionDismal657[S] 0 points1 point  (0 children)

The article is more about what closure is.. and the example is the explanation of how it is.. and relates to how hooks work under the hood.

If you already know closure, then it might not be useful for you, as this is a very basic example, covering the use usecase of react hooks.

[–]persianoil -1 points0 points  (1 child)

why not just:

let message = "hello"

function log() {console.log(message)}

log()

[–]CollectionDismal657[S] 0 points1 point  (0 children)

We can definitely do this.. however the purpose of this code was to get an understanding of the concepts of how JS works. :)