Original talk "Getting closure on React Hooks" by Shawn "swyx" Wang at JSConf.Asia 2019 in Singapore. Find it at • Can Swyx recreate Reac...
Пікірлер: 83
@FERnandezJjuan Жыл бұрын
That it’s not a HOOKk it’s just an state manager, it’s just taking advantage of closure
@ViieeS Жыл бұрын
Can’t imagine why I need hooks in Node 😅
@YGaming18 Жыл бұрын
Wtf
@okopyl Жыл бұрын
Can’t imagine why I need node to except Vercel when we all have Python
@ViieeS Жыл бұрын
@@okopyl depends of use cases, it’s a powerful tool, I use both on my projects
@sebastiangudino9377 Жыл бұрын
useState and use effect are a way to achieve (functional) reactive programing (Which is the reactive that gives REACT it's name) It's a very usefull pattern that can simply state management a lot.
@oleh110 ай бұрын
@@sebastiangudino9377useState and useEffect are hacks because of React's poor design. The proper way to implement reactivity is to use an EventEmitter or an observer pattern(implemented by rxjs for example)
@arsheonix5893 Жыл бұрын
feels like he's cloning createSignal in SolidJs
@RedStone576 Жыл бұрын
this is just glorified mutable variable
@sebastiangudino9377 Жыл бұрын
Yeah, which is what useState is (Except that in react I also triggers a re-render). But pair it with useEffect, and now that variable can ejecute a function everytime it changes. And you have achieved reactivity. Which is pretty great
@oShinobu9 ай бұрын
@@sebastiangudino9377 This is literally classes with getters and setters except more confusing
@sebastiangudino93779 ай бұрын
@@oShinobu Yeah! "Reactivity" is just calling a function on getters and setters. It just so happens that such a simple concept gives you "Inmutable state". Which works well with functional programming and allows for very a very debuggable experience for projects with lots of state. Which is why front end frameworks use them
@pranjalagnihotri60723 ай бұрын
Watched this back in college was amazing 😊
@marcusrehn6915 Жыл бұрын
When faced with a difficult problem, find a simpler one and solve that instead. This is not useState.
@oleksandrploskovytskyy1520 Жыл бұрын
Better to say it’s custom implementation of signals rather that hooks
@wlockuz4467 Жыл бұрын
This is also not a signal. Its just an example of a closure.
@jma42 Жыл бұрын
Its not a hook, its a signal implementation react what does there instead is save the state outside from the function, so when the hook runs again (when a component rerenders) it gets the new state. Something like this ```js let state = 0 ; function useState(s) { state = state || s const setState = (setter) => { state = setter(state) } return [state, setState] } const [count, setCount] = useState(1); console.log("initial state", count); setCount((n) => n + 1); console.log("stale state", count); // rerender const [count2, setCount2] = useState(1); console.log("changed state", count2); ```
@1conscience0dimension11 ай бұрын
love you
@jeetviramgama4392 Жыл бұрын
Can it be an example of a closure?
@mrajkishor331 Жыл бұрын
How?
@buddimalliyanapathirana1767 Жыл бұрын
@@mrajkishor331 the state variable can be accessed after returning .
@rgrannell1 Жыл бұрын
Yes I believe so
@abhinandanchatterjee73159 ай бұрын
Yes. Here, after the useState function has finished execution, the setState function still retains access to its parent variable and is able to do manipulations on it. So it can be called a closure.
@MCasterAnd Жыл бұрын
What kind of madman would want to put Reacts mess into Node?!
@sebastiangudino9377 Жыл бұрын
React is pretty cool. And represents a paradigm called reactive programing. Why is using RxJS good and React bad if the goal is reactivity in the end? (Very very very different things, I am aware, but both based arround the reactivity mindset and design patterns)
@und0 Жыл бұрын
Mess? React done right is a purely declarative user interface representation of immutable states. OOP patterns are the real messes, garbage paradigm that always lead to spaghetti 🤢.
@ufukbakan4741 Жыл бұрын
its not reacts mess, its an approach called reactive programming, and it started with RxJS not even with React
@stopPlannedObsolescence11 ай бұрын
sounds like a junior talking
@ryanlog10 ай бұрын
@@und0 React literally is a big MESS when compaed to other frameworks like Solid, Svelte, Vue etc and slower from all of them
@ShaharHarshuv Жыл бұрын
Doesn't that defeats the purpuse if you don't "rerender"? what's the point?
@maelstrom-qw1jl Жыл бұрын
Exactly. There's no concept of "re-rendering" whatsoever in a server-side context so React hooks are completely useless.
@sebastiangudino9377 Жыл бұрын
Your render function can do other things. Like updating the database every time the variable changes This also opens the possibility for a useEffect hook. That triggers a function every time a variable gets updated. This would be a nice alternative to the observer pattern and things like RxJS Hooks are a weird idea in node. And there are other ways to archive things like reactivity there (Like the aforementioned RxJS). But it is really not a bad idea! It's worth exploring
@ShaharHarshuv Жыл бұрын
@@sebastiangudino9377Based on the example in this video, it's unclear how you would do such a thing. Assuming that you don't really want to touch the "useState" imlementation? One interesting thing that actually could be useful to replace rxjs in simple scenarios is signals. I've been looking at Angular's implementation lately at that looks pretty good. They manage to create connections between signals and effects because when you run a "compute" or "effect" call back they register a reference to that "node" so every signal you tried to read from is registered as a dependency of that node, so the subscription happens automatically. It's basically an easier version of rxjs that is easier to read and works for simple scenarios
@ganeshtowar8050 Жыл бұрын
Where can we see full videos of this
@siyahulhaq706210 ай бұрын
Use state in react using usereducer under the hood and reducer is managing re renders
@ryanlog10 ай бұрын
This is SolidJS actually
@martybando1668 Жыл бұрын
so convoluted
@MrPlaiedes Жыл бұрын
So it's solid js...
@comadeycluster Жыл бұрын
Font ??
@shantanusharma4901 Жыл бұрын
Very good
@hasanassidiqin949710 ай бұрын
Return
@top-notchdiscovery1920 Жыл бұрын
I'd call it useSignal
@afrid189 ай бұрын
What font is it?
@target500milliontradersinv511 ай бұрын
Ful lecture Vedio link?
@GamingGeek9000 Жыл бұрын
Epic
@MarianoAquino Жыл бұрын
looks like FP discovered OOP =P
@ProtectedClassTest Жыл бұрын
react devs are so crazy
@spookyconnolly6072 Жыл бұрын
@@ProtectedClassTest this sort of carry-on has been in literature for lisp/scheme/FP languages like SICP for decades. There is an old joke at MIT -- that i will not repeat in full -- where an initiate talks to a yogi and tries to get an answer whether closures or objects are better.
@princesiachin279 Жыл бұрын
❤
@thomasneal71269 ай бұрын
I concur
@thanirmalai Жыл бұрын
But how to trigger a render on to the screen when state is changed
@EatMyHacks Жыл бұрын
useEffect!
@redhook777 Жыл бұрын
Call the virtual comparison function for virtual DOM inside setState function
@novanoskillz4151 Жыл бұрын
@@EatMyHacks so now he needs a video on how to recreate useEffect for node.
@basepasandhai1906 Жыл бұрын
React automatically re render components on state changes
@georgehammond8678 ай бұрын
That's is really hard software
@Cookie-mv2hg9 ай бұрын
What on earth is the font he is using ?
@awekeningbro12079 ай бұрын
Fira code
Жыл бұрын
that’s not how hooks work! with hook these 4 LoCs will print out 1 and 1
@kavehtehrani10 ай бұрын
That was amazing!
@hareeshbhittam7243 Жыл бұрын
What is the name of the speaker?
@raijinhasarrived Жыл бұрын
You can try to check in description
@buddimalliyanapathirana1767 Жыл бұрын
@@raijinhasarrived can't see description on the phone
@raijinhasarrived Жыл бұрын
@@buddimalliyanapathirana1767 I heard a legend about 3 dots on top right corner
@Nonsense116 Жыл бұрын
Lol why would you ever want to bring in reacts insanity into a non UI world
@helloluiis Жыл бұрын
I’m in frontend, this is one of those times where frontend concepts just need to stay in the frontend 😅
@rahulpatil1382 Жыл бұрын
what is good to use useState in node js that venila js i am new to this and want to know more
@hodev632 Жыл бұрын
Making imutable setter and getter
@Khaled-bs7zc Жыл бұрын
Guys don't take this video seriously. It's meant to be serious but it's a waste of time.
@greendsnow8 ай бұрын
Can you please clone svelte too? Because react state management is awful
@ponysmallhorse9 ай бұрын
I am a simple man. I see JS content I downvote! Python sends its regards.
@nathanhettige8465 Жыл бұрын
Bruh
@undefined_cat Жыл бұрын
It makes no sense
@Thassalocracy Жыл бұрын
If you have to call "count" as a function then effectively you're creating a signal which means you're cloning SolidJS and not React. 😏
@complexity5545 Жыл бұрын
This is why javascript sucks. So many gotcha's. I feel sorry for new programmers. You guys are taught the bad stuff from the beginning.
@zheil9152 Жыл бұрын
How to slow down you performance of node code 101
@hasanassidiqin949710 ай бұрын
Usestate
@TheScriptPunk8 ай бұрын
Lets normalize not using ameteur frameworks my guy. Node is awful as are the libs that exist within it