npm install
npm start
- Counter with offset. Everything's fine
- Batching
- Async increment
- Open modal / close modal
- Increment on close (onClose callback)
- Content with self-close button
--> Realize that passing closures around may not be the best design choice
-
useState
: Always usesetCount((prev) => prev + 1)
type of invocation when possible -
Closures are like a snapshot of the state around. This is error-prone, use a different strategy if possible
- In our case extract modal 'content' into a component and
useModal()
inside it will solve the problem
- In our case extract modal 'content' into a component and
-
Use useRef instead of useState might solve some problem
- But you might encounter the same problem if you do this:
const stack = stackRef.current const doSomething = () => { console.log(stack) }