React · Senior · Question 26 of 75
What's new about callback refs in React 19?
- ✓Correct answer: The callback can return a cleanup function called when the element detaches
- 2Callback refs are no longer called with
nulland aren't needed for DOM - 3Callback refs are called on every render, not only on mount
Explanation
<div ref={node => { const obs = observe(node); return () => obs.disconnect(); }}> - the cleanup runs when the element is removed or the callback changes. Previously React called the callback with null, and cleanup had to be written by hand.