JavaScript Closures in React Hooks

  Рет қаралды 177,882

JSConf

JSConf

Жыл бұрын

Original talk "Getting Closure on React Hooks" by Shawn "swyx" Wang at JSConf.Asia 2019 in Singapore. Find it at • Can Swyx recreate Reac...

Пікірлер: 163
@OrPhEeUs
@OrPhEeUs Жыл бұрын
Don't hate on w3s, it's much more approachable for beginners than other resources.
@download333
@download333 Жыл бұрын
It's gotten a lot better than it was. Like 12 years ago it was just a useless content farm taking up the top google results. Now it's got a lot more useful information.
@dogakorkmaz213
@dogakorkmaz213 Жыл бұрын
Im loving it
@ZOCCOK
@ZOCCOK Жыл бұрын
As a complete beginner I have found it to be extremely useful, don't know how it was in the past, but today it is amazing.
@md.mohaiminulislam9618
@md.mohaiminulislam9618 Жыл бұрын
It still does not go into details. The try it yourself section is super useful. But need to go to mdn, if I really want to learn about a topic.
@aantoniou96
@aantoniou96 Жыл бұрын
​@@ZOCCOK As a beginner you don't (by definition) know what a good resource is. W3Schools is not one of them.
@boredomindex3353
@boredomindex3353 Жыл бұрын
He is the only person I know who could explain closures in less than 3 mins. Thank you!!!
@fieryscorpion
@fieryscorpion Жыл бұрын
C’mon bro, Don’t hate on w3 schools. That site’s great!
@josephjasonbuhain8253
@josephjasonbuhain8253 Жыл бұрын
Nice insight but w3s is very useful especially for beginners though.
@radoslavsk8591
@radoslavsk8591 Жыл бұрын
Only its seo is wonderful, the .q of info is misserable
@TJ-hs1qm
@TJ-hs1qm Жыл бұрын
congrats you invented a class
@smilefriend7171
@smilefriend7171 Жыл бұрын
lol
@SoyAmurita
@SoyAmurita 9 ай бұрын
As android developer i understood the thing he explains once i saw ur comment
@mikeandrewfernandez9797
@mikeandrewfernandez9797 Жыл бұрын
Yo, I've been trying to read 3 pdfs and multiple web resources for this closure thing. But only this short video was able to touch my comprehension in terms of closure. Bro, thank you!
@j.r.r.tolkien8724
@j.r.r.tolkien8724 Жыл бұрын
W3S is great for getting a quick refresher and testing things out.
@alfanzain9031
@alfanzain9031 11 ай бұрын
Don't hate w3s. Hate people who use FOO and BAR as variable
@ShiloBuff
@ShiloBuff 11 ай бұрын
W3School came out way after I learned programming. But I'm no fool, it's actually a pretty useful website. Not only for simple learning, but also can be useful for API/documentation. The layout makes things pretty easy to read tool.
@octubre_lilaka
@octubre_lilaka Жыл бұрын
I know people don't really like w3s, but w3s was really helpful for me when I started programming
@ninhdang1106
@ninhdang1106 Жыл бұрын
I just got asked this question in the interview 😂 was wondering when you are even going to use it practically until I saw this video
@hellelo.5840
@hellelo.5840 11 ай бұрын
He learned from w3 schools, ungrateful, this is the type of person you shouldn't have relationship with.
@dheepshemare5833
@dheepshemare5833 Жыл бұрын
This was the best explanation!
@20cmusic
@20cmusic Жыл бұрын
Haters hate w3s.
@TOBIKOLO
@TOBIKOLO 10 ай бұрын
best short yet
@edcarlo
@edcarlo Жыл бұрын
That's how global scope works, that's why we have a global and local scope.
@iantimmis651
@iantimmis651 9 ай бұрын
"Functional programming" like this is just OO in disguise
@chetanchandel8850
@chetanchandel8850 9 ай бұрын
this is such a great explanation
@abdullohkarimov4396
@abdullohkarimov4396 Жыл бұрын
What is the full version of it?
@justafreak15able
@justafreak15able Жыл бұрын
it gets shit done
@SH-zf7in
@SH-zf7in Жыл бұрын
Don't roast w3s like that 😢😢😢😭
@mfsbo
@mfsbo Жыл бұрын
Commenting to remember this is great example
@caasieu
@caasieu 6 ай бұрын
This is the ONE!
@manjunathkadlimatti3245
@manjunathkadlimatti3245 Жыл бұрын
The audacity to disrespect w3schools
@simomoujami5616
@simomoujami5616 Жыл бұрын
Too bad I was looking for Closures in Angular, but this doesn't apply because it's for "React Hooks".
@yachujoshi
@yachujoshi Жыл бұрын
What series is this? Can someone provide me the source?
@shubitoxX
@shubitoxX Жыл бұрын
It's in the description kzbin.info/www/bejne/gXuzYnhjjpKrr9E
@savire.ergheiz
@savire.ergheiz Жыл бұрын
Hate it or not w3s has its own shiny time and still persist to this day 😁
@31redorange08
@31redorange08 Жыл бұрын
Wrong title? React hooks?
@invinciblemode
@invinciblemode Жыл бұрын
This is the basis for how react hooks work dummy
@31redorange08
@31redorange08 Жыл бұрын
@@invinciblemode Sure.
@ayomidediekola2505
@ayomidediekola2505 Жыл бұрын
​@@invinciblemode It wouldn't kill you to reply without the “dummy”, js.
@invinciblemode
@invinciblemode Жыл бұрын
@@ayomidediekola2505 yeah but it’s less fun.
@xpointer483
@xpointer483 4 ай бұрын
@@invinciblemodeDisrespecting someone is not fun. Grow up.
@jonopens
@jonopens Жыл бұрын
Literally nothing to do with react hooks from the context of the clip
@inigocilveti3287
@inigocilveti3287 Жыл бұрын
Except that's exactly how hooks work lmao
@YouTubePL666
@YouTubePL666 Жыл бұрын
@@inigocilveti3287 except that closures are way older that hooks :P I wonder when they will discover currying....
@xpointer483
@xpointer483 4 ай бұрын
@@inigocilveti3287Plz explain how are hooks formed that way. The only thing that I can think of is that, the components behave with this same concept. The child components’ variables and functions are scoped away from its outer (parent) or sibling component which is why we can risk naming functions with the same name as we have in parent without any consequence.
@inigocilveti3287
@inigocilveti3287 4 ай бұрын
@@xpointer483 kzbin.info/www/bejne/gXuzYnhjjpKrr9E&ab_channel=JSConf You can take a look at that video to see what I mean. Closures ar a key concept to understand how hooks work.
@albertoyoldan288
@albertoyoldan288 Жыл бұрын
How does this related to recursion is this the same method as closure?
@milesstripling373
@milesstripling373 Жыл бұрын
Not familiar with js, but when you use the keyword “let”, i know that is to declare a mutable variable. However, when you call getAdd(), why doesn’t foo go back to 1? Is it something to do with the const add = getAdd()?
@priapulida
@priapulida Жыл бұрын
it's only called once, which means foo is only set to 1 once. the const just means the name "add" can't be reassigned, but the content of non-primitives still can change, in this case the foo variable in the function object "add"
@szalaytamas3184
@szalaytamas3184 Жыл бұрын
​@@priapulidahow is it run only once?
@szalaytamas3184
@szalaytamas3184 Жыл бұрын
​@@priapulidanvm I gotit
@emjizone
@emjizone Жыл бұрын
Mathematicians: "stateful functions" aren't functions.
@Aubregines
@Aubregines 9 ай бұрын
Made me choke and realize that I will never ever touch this atrocity ever
@emjizone
@emjizone 9 ай бұрын
@@Aubregines What "atrocity" ? "stateful" functions (methods) ? Closures ? JavaScript ? Math ? Shared variables ? Global variables ? Private variable ? KZbin ? Your keyboard ? What ?
@Aubregines
@Aubregines 9 ай бұрын
@@emjizone stateful functions, but JS is quite high in the atrocity scale as well
@emjizone
@emjizone 9 ай бұрын
​@@Aubregines closure is a highly valuable concept both in math and computer science, and objects with states and methods are indeed very useful to build and deploy counters and interfaces without too much scope, stability and portability issues. The pattern isn't the issue, here. It's silly use might be in some cases, of course, but that's another debate. What is terrible is the use of the word "function" for something that is clearly *not* a function by definition. And this is not specific to _JS_ at all. It's a bad habit inherited from the _C_ and _Pascal_ era. You'd have to look for fanatically function-oriented languages like _Lisp_ , _Haskell_ or _Mathematica_ to start seeing implementations of things which physical state is so well hidden and abstracted out of the syntax that you can start treating them like actual functions (compose them, reciprocate them, etc…) without cumulating documented or visible effects. But remember that *the whole point of programming is to get some effects* in the end, meaning *we are, in fact, only dealing with states,* no matter how much the language abstract that for you. Inside the machine and in all parts of the universe in general, "stateless" isn't a concrete thing, just an idea.
@ShaharHarshuv
@ShaharHarshuv Жыл бұрын
Functional programming is fun 🎉
@j.r.r.tolkien8724
@j.r.r.tolkien8724 Жыл бұрын
Indeed.
@gauravvan
@gauravvan Жыл бұрын
So ducking cool!
@AK-vx4dy
@AK-vx4dy Жыл бұрын
Genius explanation!!! But some injustice to w3achools, maybe they time passed but 10-15 years ago it was concise knowledge source.
@edwarddejong8025
@edwarddejong8025 9 ай бұрын
Unfortunately, the to reader of code, this hidden state (and you don't know how many of them are lurking) makes it hard to reason about a program. Only in the browser IDE can you see how many closures were created. A very bad idea for maintainability.
@Euquila
@Euquila Жыл бұрын
guys, he was just cracking a joke. chill
@forgivenessforness2043
@forgivenessforness2043 10 ай бұрын
What statefullness means in this context?
@veganfoodtruck4829
@veganfoodtruck4829 9 ай бұрын
Bruh. You just figured out functions are classes in js.
@last.journey
@last.journey Жыл бұрын
I wish the some day we would be able to program web with c# more is more boys
@syamkumar3318
@syamkumar3318 Жыл бұрын
@rayeesdot310
@rayeesdot310 2 ай бұрын
now i know something
@Jade_Hanson
@Jade_Hanson Жыл бұрын
lolol W3S saves lives
@MissElyseaVlogs
@MissElyseaVlogs Жыл бұрын
🙌🏽
@lekdup_11
@lekdup_11 Жыл бұрын
🎉
@eXelerator97
@eXelerator97 Жыл бұрын
But why don't we just create a class with private fields instead?
@herman751
@herman751 Жыл бұрын
It does the same thing under the hood, classes are just syntax sugar.
@20cmusic
@20cmusic Жыл бұрын
Class doesn't really exist in javascript.
@xskrish
@xskrish Жыл бұрын
That's an Object Oriented Programming concept, JS is supposed to be functional. However with Typescript, you can create classes with private fields and everything, not saying that you can't do that in JS, it's just that JS was created to solve a different problem and shouldn't be considered a replacement for Java , say.
@hanzofuma
@hanzofuma Жыл бұрын
Some people hate OOP and want to do it the functional style way if you don't mind you can go with OOP. OOP's main concept was encapsulating states into objects this is why it existed
@deadchannel8431
@deadchannel8431 Жыл бұрын
@@20cmusic wdym class doesn’t exist in js? It does quite clearly exist
@vivekkoul4428
@vivekkoul4428 Жыл бұрын
I didn't get the last part. How const add = getadd() works? Even though foo value has changed but still everytime we call add, foo is assigned value as 1. What i am missing here?
@kirankamath5891
@kirankamath5891 Жыл бұрын
add is storing the function returned by getadd() , this function is forming a closure with outer function i.e getadd , so it has access to its local environment as well as its outer lexical environment. Since foo value is getting updated by one on every call , its updated value will be accessible on calling add();
@vivekkoul4428
@vivekkoul4428 Жыл бұрын
@@kirankamath5891 pls tell me one thing. When we return from a function, all the variables in its scope get totally removed from stack. So in this case, when we call add() first time, it returns foo =2 and after that add() is removed from stack and along with it, its local variable foo. And when we call next time add(), won't it again start from scratch i mean won't foo will be 1 again?
@kirankamath5891
@kirankamath5891 Жыл бұрын
@@vivekkoul4428 yes sir that's the beauty of closure, it will still retain the value of its local variable as well as it's lexical environment even if the outer function gets removed from stack
@vivekkoul4428
@vivekkoul4428 Жыл бұрын
@@kirankamath5891 so do you mean here we put add() deliberately inside another function to make sure local variables of add() doesn't get removed from the memory?
@kirankamath5891
@kirankamath5891 Жыл бұрын
@@vivekkoul4428 here add is the reference to that inner function which will have access to its local variable
@late.night.creation
@late.night.creation 9 ай бұрын
Can someone explain to me why “let foo = 1” doesn’t reset the value everytime the function is called?
@numoru
@numoru 9 ай бұрын
Linear it reads once
@numoru
@numoru 9 ай бұрын
Like old CRT tvs
@zeez7777
@zeez7777 Жыл бұрын
If you think this is enhancing security in any way opposed to making it harder for yourself to do mistakes then you are mistaken.
@jikaikas
@jikaikas Жыл бұрын
Looks cool , whats the application for this
@darentok8608
@darentok8608 Жыл бұрын
React hooks, like useState
@darentok8608
@darentok8608 Жыл бұрын
Like how it works.
@iSaac-kp5lk
@iSaac-kp5lk Жыл бұрын
What about const
@Jandrei365
@Jandrei365 Жыл бұрын
Can I present Java to you?
@Godrose
@Godrose Жыл бұрын
😂
@dafivers4127
@dafivers4127 10 ай бұрын
W3s is fire this guy lost all credibility with that line.
@pguti778
@pguti778 Жыл бұрын
Dislike because of the W3S thing
@sourabhkulkarni1731
@sourabhkulkarni1731 Жыл бұрын
Best explanation of closure !!!!
@charlesthompson8938
@charlesthompson8938 Жыл бұрын
Then you must have used W3S only once…
@ezekielflores2114
@ezekielflores2114 11 ай бұрын
Foo = foo 🤔 what my head is shaking
@DillonRedding
@DillonRedding Жыл бұрын
*laughs in OO*
@user-ti1kc7qk3n
@user-ti1kc7qk3n Жыл бұрын
나는 W3S 좋아하는데...
@guillemgarcia3630
@guillemgarcia3630 Жыл бұрын
hey there, gonna blow your mind you can do this, and you skip a closure let dosomething { let a = 3 something = () => { a += 1 console.log(a) } } no need to create useless js anon funcs
@warguy6474
@warguy6474 9 ай бұрын
w3 is great if you dont want all the bullshit and you just quickly need to know syntax
@numoru
@numoru 9 ай бұрын
Chat got better
@coolemur976
@coolemur976 Жыл бұрын
Ah yes... this is a great example of functional programming that tries to implement OOP patterns. State in functions is not intuitive. This is a job for classes and private fields. Some may argue, that it's just a syntax sugar. Well, at least it's more intuitive way to structure your code. Unless you want to spend time searching for "hidden features" in your functions...
@mrajkishor331
@mrajkishor331 Жыл бұрын
One like for w3schools
@cscs6587
@cscs6587 Жыл бұрын
Easily
@ogawde7
@ogawde7 10 ай бұрын
switch to ts
@sohanagate3037
@sohanagate3037 Жыл бұрын
Cfbr
@yognaut
@yognaut Жыл бұрын
Alternative way to do this, just use a scope block: { let foo = 1 function getAdd() { return ++foo } } getAdd() Saves the extra function declaration
@NexushasTaken
@NexushasTaken Жыл бұрын
so, you can access the function inside a block from outside?
@YouTubePL666
@YouTubePL666 Жыл бұрын
No this is a very bad example as this behaviour is inconsistent across browsers and is in fact incorrect. "use strict"; will make that function declaration scoped locally, so you will get Reference Error which is the correct behaviour
Жыл бұрын
@@KZbinPL666 even if we declare a variable with var then assign the getAdd function? is it still considered bad practice?
@coomservative
@coomservative Жыл бұрын
this is how debounce works
@54peace
@54peace Жыл бұрын
Hater spoted.
@charlesbinet2301
@charlesbinet2301 Жыл бұрын
Or use classes
@chologhuribangladesh7792
@chologhuribangladesh7792 Жыл бұрын
W3Schools fan be like 😡😡
@hasanassidiqin9497
@hasanassidiqin9497 10 ай бұрын
Console
@ash_177
@ash_177 Жыл бұрын
Dude don't bash w3 schools, I got 3 stars on JS in w3 schools :
@ytadi9229
@ytadi9229 Жыл бұрын
Ain't w3schools have fixed it's content now?
@tomjones8293
@tomjones8293 10 ай бұрын
we all understand closure easy.. but what is it for ? where is it used in real world example... what real problem is it solved with real example e.g a framework uses it to solve this that ? useless
@furkannarin2844
@furkannarin2844 Жыл бұрын
PUHAHAHAHA and you cant mess because it is still out of scope lol
@Alguem387
@Alguem387 Жыл бұрын
That's just a class but worse
@urssaf343
@urssaf343 Жыл бұрын
Then there's foobar examples... smh
@rankarat
@rankarat Жыл бұрын
React is the most overrated framework out there. There are much better frameworks than Facebook one.
@WiseWeeabo
@WiseWeeabo Жыл бұрын
please never do this
@yoni532s9M5w
@yoni532s9M5w Жыл бұрын
Why foo and not num? Bad practice
@hza.609
@hza.609 Жыл бұрын
ugly fn in fn..... js .....
@NexushasTaken
@NexushasTaken Жыл бұрын
damn, js script is so weird.
@dustinscroggins6256
@dustinscroggins6256 Жыл бұрын
Lmaoo
@ewdlop1
@ewdlop1 Жыл бұрын
nothing to do with JScript it is FP
@kevklash
@kevklash Жыл бұрын
You definitely DON'T trust a guy that says they have found W3S useful just once.
@infeza3255
@infeza3255 Жыл бұрын
best short yet
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 450 М.
Learn React Hooks: useEffect - Simply Explained!
14:07
Cosden Solutions
Рет қаралды 108 М.
[柴犬ASMR]曼玉Manyu&小白Bai 毛发护理Spa asmr
01:00
是曼玉不是鳗鱼
Рет қаралды 49 МЛН
UFC 302 : Махачев VS Порье
02:54
Setanta Sports UFC
Рет қаралды 994 М.
How Slow Is JavaScript? | Prime Reacts
15:34
ThePrimeTime
Рет қаралды 168 М.
Closure in Javascript
14:55
Piyush Garg
Рет қаралды 14 М.
Asynchrony: Under the Hood - Shelley Vohr - JSConf EU
25:24
Learn JavaScript CLOSURES in 10 minutes! 🔒
10:58
Bro Code
Рет қаралды 11 М.
All useEffect Mistakes Every Junior React Developer Makes
22:23
The Story of Next.js
12:13
uidotdev
Рет қаралды 532 М.
Learn React Hooks: useMemo - Simply Explained!
13:41
Cosden Solutions
Рет қаралды 75 М.
JavaScript Closures in FIVE Minutes
5:00
James Q Quick
Рет қаралды 25 М.
wyłącznik
0:50
Panele Fotowoltaiczne
Рет қаралды 23 МЛН
Топ-3 суперкрутых ПК из CompShop
1:00
CompShop Shorts
Рет қаралды 333 М.
How charged your battery?
0:14
V.A. show / Магика
Рет қаралды 2,9 МЛН
Pratik Cat6 kablo soyma
0:15
Elektrik-Elektronik
Рет қаралды 8 МЛН
Samsung or iPhone
0:19
rishton vines😇
Рет қаралды 9 МЛН