Skip to content

Commit

Permalink
made the ref extensible for the forward ref scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
Biki-das committed Jan 22, 2025
1 parent 94387b6 commit 8acf0a8
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/react-refresh/src/__tests__/ReactFresh-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -902,8 +902,22 @@ describe('ReactFresh', () => {
await patch(() => {
const Test = React.forwardRef((props, ref) => {
const [count, setCount] = React.useState(0);
const handleClick = () => setCount(c => c + 1);

// Ensure ref is extensible
const divRef = React.useRef(null);
React.useEffect(() => {
if (ref) {
if (typeof ref === 'function') {
ref(divRef.current);
} else if (Object.isExtensible(ref)) {
ref.current = divRef.current;
}
}
}, [ref]);

return (
<div ref={ref} onClick={() => setCount(c => c + 1)}>
<div ref={divRef} onClick={handleClick}>
count: {count}
</div>
);
Expand Down

0 comments on commit 8acf0a8

Please sign in to comment.