From 51a19cd30aa4f9020a0cd8c558bfd11415970a7b Mon Sep 17 00:00:00 2001 From: Jack Pope Date: Fri, 19 Apr 2024 15:29:09 -0400 Subject: [PATCH] Remove warning for ref cleanup function --- packages/react-dom/src/__tests__/refs-test.js | 41 ------------------- .../src/ReactFiberCommitWork.js | 14 +------ 2 files changed, 2 insertions(+), 53 deletions(-) diff --git a/packages/react-dom/src/__tests__/refs-test.js b/packages/react-dom/src/__tests__/refs-test.js index a24bdf481e16f..577d91f38f368 100644 --- a/packages/react-dom/src/__tests__/refs-test.js +++ b/packages/react-dom/src/__tests__/refs-test.js @@ -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( -
{ - setup(_ref); - if (returnCleanUp) { - return cleanUp; - } - }} - />, - ); - }); - - expect(setup).toHaveBeenCalledTimes(1); - expect(cleanUp).toHaveBeenCalledTimes(0); - - returnCleanUp = true; - - await expect(async () => { - await act(() => { - root.render( -
{ - setup(_ref); - if (returnCleanUp) { - return cleanUp; - } - }} - />, - ); - }); - }).toErrorDev('Unexpected return value from a callback ref in div'); - }); }); diff --git a/packages/react-reconciler/src/ReactFiberCommitWork.js b/packages/react-reconciler/src/ReactFiberCommitWork.js index 87cbb2c13487c..4342c1fa5e125 100644 --- a/packages/react-reconciler/src/ReactFiberCommitWork.js +++ b/packages/react-reconciler/src/ReactFiberCommitWork.js @@ -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;