Skip to content

Commit

Permalink
Remove warning for ref cleanup function (#28883)
Browse files Browse the repository at this point in the history
Resources
- RFC: reactjs/rfcs#205
- Warning implemented in #22313
- Warning enabled in #23145
- Feature added in #25686

We have warned to prevent the old behavior since 18.0.0.

The new feature has been on in canary for a while but still triggering
the warning. This PR cleans up the warning for 19
  • Loading branch information
jackpope committed Apr 22, 2024
1 parent 5b903cd commit db913d8
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 53 deletions.
41 changes: 0 additions & 41 deletions packages/react-dom/src/__tests__/refs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,45 +706,4 @@ describe('refs return clean up function', () => {
expect(setup).toHaveBeenCalledTimes(1);
expect(cleanUp).toHaveBeenCalledTimes(1);
});

it('warns if clean up function is returned when called with null', async () => {
const container = document.createElement('div');
const cleanUp = jest.fn();
const setup = jest.fn();
let returnCleanUp = false;

const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<div
ref={_ref => {
setup(_ref);
if (returnCleanUp) {
return cleanUp;
}
}}
/>,
);
});

expect(setup).toHaveBeenCalledTimes(1);
expect(cleanUp).toHaveBeenCalledTimes(0);

returnCleanUp = true;

await expect(async () => {
await act(() => {
root.render(
<div
ref={_ref => {
setup(_ref);
if (returnCleanUp) {
return cleanUp;
}
}}
/>,
);
});
}).toErrorDev('Unexpected return value from a callback ref in div');
});
});
14 changes: 2 additions & 12 deletions packages/react-reconciler/src/ReactFiberCommitWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,30 +318,20 @@ function safelyDetachRef(current: Fiber, nearestMountedAncestor: Fiber | null) {
}
}
} else if (typeof ref === 'function') {
let retVal;
try {
if (shouldProfile(current)) {
try {
startLayoutEffectTimer();
retVal = ref(null);
ref(null);
} finally {
recordLayoutEffectDuration(current);
}
} else {
retVal = ref(null);
ref(null);
}
} catch (error) {
captureCommitPhaseError(current, nearestMountedAncestor, error);
}
if (__DEV__) {
if (typeof retVal === 'function') {
console.error(
'Unexpected return value from a callback ref in %s. ' +
'A callback ref should not return a function.',
getComponentNameFromFiber(current),
);
}
}
} else {
// $FlowFixMe[incompatible-use] unable to narrow type to RefObject
ref.current = null;
Expand Down

0 comments on commit db913d8

Please sign in to comment.