4 ms·
Do you have a reduced code sample? I can’t quite picture the stale state issue. (I suppose this is a good example in favour of the opposing “useCallback is pre
by tpict 4y ago
Do you have a reduced code sample? I can’t quite picture the stale state issue.
(I suppose this is a good example in favour of the opposing “useCallback is premature optimisation” opinion)
- Rodeoclash 4y agoNot on hand, but I'll put together one in a codepen now.
- Rodeoclash 4y agoThis is the exact footgun I ran into yesterday: https://codepen.io/rodeoclash/pen/poLqeYb?editors=1111 https://codepen.io/rodeoclash/pen/poLqeYb?editors=1111 Even though `theState` is passed to the dependency array of the parent component which is tracking hidden images, it will still be stale! After a while, I figured out that the child components, where the click originates from, also needed to have the map of deleted images passed to their dependency array. Total footgun.
- zastrowm 4y agoFor this specific example the problem is that the handleClick callback is using a stale/cached onImageDeleted callback (which indirectly has old state). If you add onImageDeleted to the handleClick dependency array, it beings to work as you expect.
- tylerhou 4y agoWould the dependency linter catch this? I assume yes?
- tpict 4y agoYep, it will flag any references to variables in the outer scope that it doesn’t know for certain are stable.
- Rodeoclash 4y agoAhh, that's interesting.