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

The next step is for the ref prop to accept an array and only execute the callbacks that changed between renders, instead of all of them.

One of my favorite changes in v19. Callback refs are really good. I'll write another blogpost soon because my recommendation to wrap in useCallback might not be 100% correct.

You won't have access to things created in the callback when it gets called again with null. You can store in a ref but this is really a lot better.

Yeah, I'm curious about that. I recognize this pattern from SolidJS but I imagine React will run this ref callback on every re-render. Could that be an issue for, say, `document.addEventListener` use cases?

If you can extract the function outside of the component, you won't have that issue. If your code still runs correctly (just not very performant), wrap in useCallback.