Why "Stale Closure" Misleads React Devs

Поделиться
HTML-код
  • Опубликовано: 19 окт 2024

Комментарии • 24

  • @zzzuq
    @zzzuq 2 дня назад +20

    SOMEONE GIVE THIS MAN A RAISE

    • @UIEngineering
      @UIEngineering  2 дня назад +1

      +1 💸 haha, would be great, thanks! 😆

  • @turolretar
    @turolretar День назад +6

    About your example of useState - yes, this is exactly how it works with primitive values, now let’s try it with an object, it will mutate immediately, because we get objects by reference and primitives by value in JS. React, as you mentioned, copies the objects in order to make state immutable, which is a little different.

  • @ThiagoVieira91
    @ThiagoVieira91 2 дня назад +3

    I deallocated my lunch just to watch this video. Outstanding!

    • @UIEngineering
      @UIEngineering  2 дня назад +1

      🫶 Hope your stomach isn't unsubscribing from you now 😂

  • @turolretar
    @turolretar День назад +1

    I like how creators of react don’t even know how their creation works. That makes sense

  • @turolretar
    @turolretar День назад +1

    I think what they meant was not javascript closures, the phrase “stale closure” is just a handy way of describing that we have the old references to our objects during a certain React render. In regular closures there is nothing to get stale - it doesn’t even make sense. But react renders by evaluating the render function every time, ergo recreating objects every time (if not using memoization). The object name is the same, but the reference is of course different (except when we useRef). This staleness is important, for example when setting new state derived from its current value in an event handler. Since event handlers run immediately, they are not synced with the render loop and there can be moments where the state value has already changed but the event handler has not and still has that old one. There are other places like useEffect where this staleness is important to understand. This is the basis of react in my mind. I don’t think any of this is wrong or misleading, besides the weird naming by react team.

  • @ralacerda12
    @ralacerda12 2 дня назад +2

    Great video! I learned a lot and I don't even develop in React.

  • @KarimShalapy
    @KarimShalapy День назад

    AT LAST SOMEONE SAID IT!!!

  • @RAMPAVAN90
    @RAMPAVAN90 2 дня назад +1

    Fantastic dude! Great work in explaning this!!

  • @knit-abroad-glide
    @knit-abroad-glide 2 дня назад +3

    I don't think both 'stale closure' or 'primitive or reference' gets to the point.
    Setting a primitive state variable to other value won't trigger the rerender, so idk it's very relevant to the topic.
    setState is kinda like queueRenderNextSnapshotWithThisUpdatedComponentDependencyValue,
    so explaining about mental model of React's reactivity system seems like a much better approach for me.

  • @Lexaire
    @Lexaire 2 дня назад

    Absolutely love your videos! When I saw this pop up in my feed I thought the title was clickbait, then I saw it was from UI Engineering and I knew this was going to be good!

    • @UIEngineering
      @UIEngineering  2 дня назад

      Damn, man. I'm honored, thanks much, really appreciate it 🙇

  • @artur-rdc
    @artur-rdc 2 дня назад

    That was a really good video, well done.

  • @aeronwolfe7072
    @aeronwolfe7072 2 дня назад

    YOU SIR, are AWESOME. I have been using React for YEARS!!!!!!!!!! I LEARNED SOMETHING NEW!!!!!!!!! You get the SUB BRO!!!!!!!

  • @kippeneneieren2039
    @kippeneneieren2039 2 дня назад +1

    The fact that this channel is not bigger is a warcrime

  • @PooyaBadiee
    @PooyaBadiee 2 дня назад

    Great Video,.
    I never got the stale closure argument anyway. I agree 100%, the framing of the issue just makes it more complex for the newcomers

  • @arnabchatterjee8556
    @arnabchatterjee8556 День назад

    Thank God , I haven't read those without understanding. My explanation was correct. But one question about the internal copying of value. let state = some object. a=state. Now if we update a, state will also get updated. How react solve this?

  • @netssrmrz
    @netssrmrz 2 дня назад

    nice video. it just reminds me of why i hate react so much. unfortunately, its how i make money. on the bright side, my need to use it professionally seems to be dwindling.

  • @vitvitvitvitvitvitvitvit
    @vitvitvitvitvitvitvitvit День назад

    damn i think i was a vid with more views. good video though

  • @kostraone
    @kostraone День назад +1

    React is just garbage 🤷