[Suggestion]: Documentation for `useRef` should tell us how to deal with multiple refs on a single element
まだ誰も着手していません。
- 主要言語
- JavaScript
- スター
- 11.8k
- フォーク
- 7.9k
- 平均マージ
- 1日 11時間
- マージ済み PR(30日)
- 11
説明
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>
);
}
);
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
https://react.dev/reference/react/useRef の現在の useRef リファレンスページから始め、レンダー中の refs についてどのように説明しているかを確認してください。Issue で説明されている multiple-ref と eslint-plugin-react-compiler に関する懸念を調査し、そのうえで、サポートされているアプローチと期待されるコンパイラの動作が明確になるようドキュメントを更新してください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- react
- 領域
- documentation
- issue の種類
- ドキュメント
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 静か
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 55/100