Why Didn't My State Update Immediately? — Understanding React #6
rge
0:00 / 0:00
Why Didn't My State Update Immediately? — Understanding React #6
15 просмотров · 2 недели назад
rge
46 подписчиков
15 просмотров · 2 недели назад
Call setState, log the state on the very next line, and you get the old value. Call setState twice in a row and you get one increment, not two. Both of those look like bugs and neither one is.
We reproduce both in React, then build mySetState ourselves — a queue and a flush, about fifteen lines — and watch the exact moment the second update overwrites the first instead of adding to it. Then the updater form, and why it's not a style preference.
Full playlist: • Understanding React
Understanding React — rebuilding React's ideas from first principles instead of memorizing the API.