Q66: How do Ref Callbacks support cleanup functions in modern React?
Ref callbacks (<div ref={(node) => { ... }} />) support returning a cleanup function. When the DOM node unmounts or changes, React executes the returned cleanup callback, mimicking the behavior of cleanup functions inside useEffect for precise element lifecycle management.
Key Takeaways
- Ref callbacks can return explicit cleanup functions.
- Runs cleanup automatically when the referenced DOM element unmounts or updates.
Knowledge Check
When does a cleanup function returned from a Ref callback execute?