[Suggestion]: Documentation for `useRef` should tell us how to deal with multiple refs on a single element
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 11.8k
- Forks
- 7.9k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 11
Description
Summary
Sometimes you need to add more than one ref to a single element. For example:
- When using
forwardRefyou need to pass the forwarded ref to whatever element, but you also need your own ref attached to the same element. - When using
useControllerofreact-hook-form, you get a ref you need to pass to the input element for focus management, but you also need your own ref for doing other things with that same input element.
This is an issue that is not even mentioned on the current documentation of useRef. I've been using gregberge/react-merge-refs to deal with this, but after I added the recommended eslint-plugin-react-compiler, this method now generates a warning:
Ref values (the
currentproperty) may not be accessed during render.
If you're not allowed to access ref values during render to merge them, then how are you supposed to attach multiple refs to a single element now?
The documentation needs to tell us how to do this properly, and in a way that doesn't break the compiler or generate warnings in your eslint plugins.
Page
https://react.dev/reference/react/useRef
Details
Example component needing this:
export default forwardRef(
function BaseCheckbox(
{ label, indeterminate = false, onChange, onCheckedChange, ...props },
ref
) {
const inputId = useId();
const inputRef = useRef(null);
useLayoutEffect(() => {
if (inputRef.current)
inputRef.current.indeterminate = indeterminate;
}, [indeterminate]);
return (
<div className="checkbox">
<input
type="checkbox"
id={inputId}
/**
* Here we have two refs we need to attach to this input, but there's no
* documented way to do that and the undocumented way is not allowed
* according to the new rules from `eslint-plugin-react-compiler`.
*/
ref={mergeRefs(inputRef, ref)}
onChange={(e) => {
onChange?.(e);
onCheckedChange?.(e.currentTarget.checked);
}}
aria-checked={
indeterminate ? 'mixed' : props.checked ? 'true' : 'false'
}
{...props}
/>
<label htmlFor={inputId}>
{label}
</label>
</div>
);
}
);
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the current useRef reference page at https://react.dev/reference/react/useRef and review how it discusses refs during render. Investigate the multiple-ref and eslint-plugin-react-compiler concerns described in the issue, then update the documentation so the supported approach and its expected compiler behavior are clear.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100