[Suggestion]: Documentation for `useRef` should tell us how to deal with multiple refs on a single element
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 55/100
- Loại issue
- Tài liệu
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Ít trao đổi
- Công nghệ
- react
- Lĩnh vực
- documentation
Hướng nghiên cứu
Bắt đầu với trang tham chiếu useRef hiện tại tại https://react.dev/reference/react/useRef và xem xét cách trang này đề cập đến refs trong quá trình render. Điều tra các mối lo ngại về multiple-ref và eslint-plugin-react-compiler được mô tả trong issue, sau đó cập nhật tài liệu để làm rõ cách tiếp cận được hỗ trợ và hành vi dự kiến của compiler.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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>
);
}
);
- Ngôn ngữ chính
- JavaScript
- Star
- 11.8k
- Fork
- 7.9k
- Merge trung bình
- 16 giờ 6 phút
- Pull request đã merge (30 ngày)
- 7
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của reactjs/react.dev
-
type: documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
-
bug: unconfirmed
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
-
type: typos
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
-
bug: unconfirmed
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Tất cả issue của reactjs/react.dev
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Seeed-Studio/wiki-documents#5655 · 2 bình luận ·
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
capricorn86/happy-dom#2435 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
-
Edit: CW+ Đang mởchannels:edit check:passed
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
-
01 type: bug 30 needs: triage 99 tag: UX Accessibility
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100