Callback refs seemed like a strategy to migrate from string refs in React 15 to 16.2. However, I don't see benefits over ref objects except that current property can be omitted in rare cases when it's worth it.
What is current status of callback refs? Are they expected to be deprecated in React 17?
The documentation tends to recommend ref objects in general but is vague about the future of callback refs. Can we update it with a clarification?
Docs mention:
React also supports another way to set refs called “callback refs”, which gives more fine-grain control over when refs are set and unset.
(Btw I guess it should've said "fine-grained")
Callback refs are still useful for cases where you actually need to be notified when the ref is being updated. Here's one example of where that matters: https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node
Using a callback ref ensures that even if a child component displays the measured node later (e.g. in response to a click), we still get notified about it in the parent component and can update the measurements.
So there are no plans to deprecate them. They are more powerful than object refs and in some cases object refs aren't sufficient. However, these cases aren't very common so object refs are a good ergonomic alternative for majority (but not all) cases.
Feel free to send a PR to docs to explain this better, or file an issue at https://github.com/reactjs/reactjs.org for that.
Whenever you find yourself starting to write
React.useEffect(() => {
// do something with the ref
}, [ref.current]);
you probably need a callback ref instead.
Most helpful comment
Whenever you find yourself starting to write
you probably need a callback ref instead.