Post

unless refs changed while i wasn’t looking you need to wrap this in `useCallback`, otherwise it’ll call both the cleanup and setup function again every single render

Hmm isn't it the same as having a useEffect without dependency array, and then adding an empty one to it? That changes st which time / how often it runs.

What’s the use case for needing to run every render? Seems like a hacky way to do side effects in render

if you create it anonymously during render, react can’t know whether it’s actually the same function from the last render. so it cleans up the previous one and sets up the new one

I guess if I make a two-tweet post, it'll get easy to misinterpret it what I meant: if ref callbacks only ran once and used a value from state in a closure, it would use that value for its entire lifetime. subverting this is not idiomatic react

0 reposts3 likes