Hooks, Dependencies and Stale Closures | TkDodo's blog
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
- Understanding useMemo and useCallback • Josh W. Comeaujoshwcomeau.com
- You Might Not Need an Effect – Reactreact.dev
- Why React Re-Renders • Josh W. Comeaujoshwcomeau.com
- Blogged Answers: A (Mostly) Complete Guide to React Rendering Behavior · Mark's Dev Blogblog.isquaredsoftware.com
- What is Memoization? How and When to Memoize in JavaScript and Reactfreecodecamp.org
- useCallback – Reactreact.dev
- useMemo – Reactreact.dev
- Implementing React from scratchrob.directory
- React graph viscrubier.github.io
- The Interactive Guide to Rendering in Reactui.dev
- useEffect – Reactreact.dev
- Building Bulletproof React Components - Shu Dingshud.in