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

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

No replies yet.