flâneur — a map of the web's best reading

Hooks, Dependencies and Stale Closures | TkDodo's blog

tkdodo.eu · 2,247 words · saved by 1 readers

Working with closures is something that can become quite tricky, especially when dealing with hooks that have dependencies in React (think useEffect, useMemo, useCallback). A lot of bugs and frustration are tied pretty closely to that concept - even though it is nothing that React itself introduced. It's rather a language concept that hooks rely upon. I love this question from Mark Erikson: Question for the React community: Has the shift from "how does `this`/binding work?"-type questions to "why is my state stale due to closures?"-type questions made things better, worse, or just "same thing but different" ? (Inspired by the questions we see in @reactiflux daily) To me, it has gotten subjectively better. Working with this in class components was a pain, and errors did mostly show up at runtime. However, the behaviour you get due to stale closures are more subtle, and come up in more edge cases. The big advantage though is that they can be statically analyzed - and that's exactly what

Hooks, Dependencies and Stale Closures Apr 24, 2022 — JavaScript , TypeScript , ReactJs , Hooks Photo by David Sarkisov #1: Hooks, Dependencies and Stale Closures Current #2: Refs, Events and Escape Hatches No translations available. Add translation Working with closures (opens in a new window) is something that can become quite tricky, especially when dealing with hooks that have dependencies in React (think useEffect , useMemo , useCallback ). A lot of bugs and frustration are tied pretty closely to that concept - even though it is nothing that React itself introduced. It’s rather a language

Explore this link on the map →

related reading