Making setState() instant

  Рет қаралды 5,454

UI Engineering

UI Engineering

Күн бұрын

Пікірлер: 40
@sirawichvoungchuy3128
@sirawichvoungchuy3128 Күн бұрын
OMG, this guy clearly explained the blackbox part of state in React to me. I can't wait for the next video!
@johnc0de
@johnc0de 5 күн бұрын
> Tries to fix react > Automatically starts to become SolidJS
@joshuathomasbird
@joshuathomasbird 3 күн бұрын
i mean even preact has signals...
@igrb
@igrb 6 күн бұрын
very very interesting exploration, learned so much, nice
@ostadomid3355
@ostadomid3355 6 күн бұрын
Thank you for detailed explanation.💯
@andreilucasgoncalves1416
@andreilucasgoncalves1416 6 күн бұрын
Great video, I was prepared to explain here why React team decided to keep that way, but you will in the next video Unfortunately in React with hooks we have to use ref frequently to deal with stale state in useEffect and useMemo
@zzzuq
@zzzuq 6 күн бұрын
Wow, this is soo useful, thanks
@rcnhsuailsnyfiue2
@rcnhsuailsnyfiue2 7 күн бұрын
Another great video, thank you! 👏 You’re a really good teacher and explain the hidden complexities of React in a simple and clear way.
@minyoungna6642
@minyoungna6642 7 күн бұрын
Ahhhh I love getters and setters for making the code more imperative!!
@Thassalocracy
@Thassalocracy 6 күн бұрын
Great explanation about useState and useRef. In essence, it all boils down to having a very good understanding of the fundamentals of JS as a PL. This is what I don't like about frameworks sometimes - they abstract the functionality so much that we don't know why the code has to be written the way it is.
@brabbit2325
@brabbit2325 3 күн бұрын
That's clever, great video! Hoping for more
@orterves
@orterves 6 күн бұрын
Why not have the setState call return the updated value? Make the code more functional, avoid the need to .current or the getter function
@excalibur2506
@excalibur2506 5 күн бұрын
What if you want to getState multiple times at multiple places
@orterves
@orterves 5 күн бұрын
@excalibur2506 I don't understand the question. The state won't change if you don't call setState, use whatever value was last returned as much as you want? I think if someone was going to mess with reacts rendering expectations like this, a functional approach would probably be better to prevent accidentally trying to get before set
@excalibur2506
@excalibur2506 5 күн бұрын
@@orterves You call setState within some function, but you want to access it's returned value outside the function. You will end up creating another ref in that case. So why bother when you can getState
@orterves
@orterves 5 күн бұрын
@excalibur2506 my point is I think developers should think very hard about whether they really want to do that in React. Mutable state that breaks the expectations of uni-directional data flow needs to be carefully implemented
@oliversaxon8656
@oliversaxon8656 12 сағат бұрын
@@orterveschill man. This video was just an experiment
@figloalds
@figloalds 2 күн бұрын
Ah yes, a lot of laps to do what Svelte does out of the box
@vpetryniak
@vpetryniak 6 күн бұрын
Or you can just use SolidJS
@unicodefox
@unicodefox 6 күн бұрын
@preact/signals ;)
@nafg613
@nafg613 6 күн бұрын
Possible improvement: instead of two functions, return one function that gets if no parameters and sets if passed a value
@TheJobCompany
@TheJobCompany 5 күн бұрын
I prefer having two separate functions because it is generally simpler to explain and understand when reading the codebase for the first time. However, when implementing a polymorphic getter and setter function, there is a potential issue - you can accidentally make it impossible to set the value to undefined or null. This could happen because passing an undefined or null value as the parameter is treated the same as if no parameter had been passed. To be fully correct, you would have to use a unique Symbol or a unique sentinel object reference as the default parameter value to be able to distinguish those cases.
@nafg613
@nafg613 5 күн бұрын
@@TheJobCompany True. We don't have that issue in Scala ;)
@injSrc
@injSrc 4 күн бұрын
​@@TheJobCompany try checking out arguments array in function. It solves the problem
@rodrigonovais9624
@rodrigonovais9624 5 күн бұрын
1:20 Not sure if anyone remembers it but "async" does existed before the keyword async exists We have callbacks, yields, generators...
@injSrc
@injSrc 4 күн бұрын
To be honest, I was never really bothered for not being able use the updated value
@aro_matt
@aro_matt Күн бұрын
So you say that rerender of the parent rerenders the children? Is that really true statement? In the scenario I have: Function Example(){ return ( )} Will internal state updates in parent cause rerenders of the child?
@guillaumebrunerie
@guillaumebrunerie 23 сағат бұрын
Yes
@adnanearef2959
@adnanearef2959 Күн бұрын
So use basically invented a `useStatefulRef` hook.
@nuvotion-live
@nuvotion-live 6 күн бұрын
All the asynchronous stuff has made my option of react really sour over time. It seems totally unnecessary
@nuvotion-live
@nuvotion-live 6 күн бұрын
It’s also very inefficient out to the box. Memorizing adds its own technical and mental complexity, and the compiler absolutely does not even begin to solve this. I decided to rewrite my app using ImGui after 5 YEARS of using React. The performance and complexity is night and day 😮‍💨
@kratos_respawned
@kratos_respawned 5 күн бұрын
I guess we can just use flushSync
@Joshua-dc4un
@Joshua-dc4un 9 сағат бұрын
If you need the value immediately at the point where you called setState, you already have the value that you passed to setState, so use that 🤦🏽. If you need that value immediately somewhere else, then you have a wrong application architecture or maybe React isn't the right choice for your software.
@wixoooooo
@wixoooooo 6 күн бұрын
Hey, great video, man! I just wanted to note something: there is no such thing in JavaScript as 'pass by value' and 'pass by reference.' That's not how JavaScript works. Pass by value and pass by reference works in languages like c/c++ not JS.
@sohamkarandikar6726
@sohamkarandikar6726 5 күн бұрын
Could you explain a bit more as to what you mean by javascript has no pass by value/reference?
@zayne-sarutobi
@zayne-sarutobi 3 күн бұрын
Are you sure you know what the heck you're saying?
@wixoooooo
@wixoooooo 3 күн бұрын
@@zayne-sarutobi yep my friend. ;)
@wixoooooo
@wixoooooo 3 күн бұрын
​@@sohamkarandikar6726 It's really hard to explain this in a comment because the answer is broad. To fully understand it, you should read the ECMAScript specification and one of the host environment specifications, such as V8. However, I'll give a small example to help you start your investigation, which you can also test on your own. Let's say you create the following variables: let a = "xyz"; let b = "xyz"; let c = "xyz"; In this case, you are not storing three separate values in memory. Instead, you're storing a single value, "xyz", and all three variables a, b, and c reference the same location in memory. So, there are not three "xyz" values in memory, but only one. For a better test, you could create a large string: let a = "...10 MB long string"; let b = "...10 MB long string"; let c = "...10 MB long string"; In this case (if all variables are identical), you won't occupy 30MB of memory but only 10MB. All three variables will reference the same value in memory. So technically you are passing REFERENCE VALUE to a function, not a VALUE. Under the hood, V8 might optimize it like this: V8_variable_long_string = "...10 MB long string"; let a = V8_variable_long_string; let b = V8_variable_long_string; let c = V8_variable_long_string; As you can see, all the variables share the same reference. Although when looking at your code, you might think you have created three independent variables with independent values. So, in any case, you are passing references or references value to a function, not actual values. It doesn't matter if you're passing a primitive value, an object, an array, or even another function-what gets passed is the reference to the value or reference to expression etc.
Goodbye, forwardRef
15:59
UI Engineering
Рет қаралды 8 М.
Stop Using FirstOrDefault in .NET! | Code Cop #021
12:54
Nick Chapsas
Рет қаралды 48 М.
Men Vs Women Survive The Wilderness For $500,000
31:48
MrBeast
Рет қаралды 98 МЛН
escape in roblox in real life
00:13
Kan Andrey
Рет қаралды 72 МЛН
Most Useful Constructs Across Programming Languages
8:33
Kantan Coding
Рет қаралды 22 М.
Let's Fix The Fatal Flaw in TypeScript's Union Types!
13:45
Typed Rocks
Рет қаралды 18 М.
From Idea To Robust Database Design
16:21
Ljupche Vasilev
Рет қаралды 18 М.
The new web API that nobody's talking about
25:35
Theo - t3․gg
Рет қаралды 63 М.
Real 10x Programmers Are SLOW To Write Code
14:51
Thriving Technologist
Рет қаралды 27 М.
Why Everyone Loves Zustand
29:27
Theo - t3․gg
Рет қаралды 72 М.
Node.js Doesn’t Suck Anymore
16:59
Web Dev Simplified
Рет қаралды 96 М.
Microservices Gone Wrong at DoorDash
17:22
NeetCodeIO
Рет қаралды 67 М.
SurrealDB 2.0
30:08
SurrealDB
Рет қаралды 28 М.