From c3b283964108b0e8dbcf1f9eb2e7e67815e39dfb Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Tue, 23 Apr 2024 16:15:13 -0400 Subject: [PATCH] Add deprecation warning for findDOMNode This is removed in version 19. We already warned inside of Strict Mode but this adds the warning everywhere. --- packages/react-dom/src/client/ReactDOMLegacy.js | 12 ++++++++++++ scripts/jest/shouldIgnoreConsoleError.js | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/react-dom/src/client/ReactDOMLegacy.js b/packages/react-dom/src/client/ReactDOMLegacy.js index af0e35e128bd..78a8a4ebd767 100644 --- a/packages/react-dom/src/client/ReactDOMLegacy.js +++ b/packages/react-dom/src/client/ReactDOMLegacy.js @@ -238,10 +238,22 @@ function legacyRenderSubtreeIntoContainer( return getPublicRootInstance(root); } +let didWarnAboutFindDOMNode = false; + export function findDOMNode( componentOrElement: Element | ?React$Component, ): null | Element | Text { if (__DEV__) { + if (!didWarnAboutFindDOMNode) { + didWarnAboutFindDOMNode = true; + console.error( + 'findDOMNode is deprecated and will be removed in the next major ' + + 'release. Instead, add a ref directly to the element you want ' + + 'to reference. Learn more about using refs safely here: ' + + 'https://reactjs.org/link/strict-mode-find-node', + ); + } + const owner = (ReactCurrentOwner.current: any); if (owner !== null && owner.stateNode !== null) { const warnedAboutRefsInRender = owner.stateNode._warnedAboutRefsInRender; diff --git a/scripts/jest/shouldIgnoreConsoleError.js b/scripts/jest/shouldIgnoreConsoleError.js index cbe2cfb6180b..72c5eba2f9a0 100644 --- a/scripts/jest/shouldIgnoreConsoleError.js +++ b/scripts/jest/shouldIgnoreConsoleError.js @@ -31,7 +31,8 @@ module.exports = function shouldIgnoreConsoleError(format, args) { 'uses the legacy childContextTypes API which is no longer supported and will be removed' ) !== -1 || format.indexOf('ReactDOMTestUtils is deprecated') !== -1 || - format.indexOf('`ReactDOMTestUtils.act` is deprecated') !== -1 + format.indexOf('`ReactDOMTestUtils.act` is deprecated') !== -1 || + format.indexOf('findDOMNode is deprecated and will be removed') !== -1 ) { // This is a backported warning. In `main`, there's a different warning // (and it's fully tested). Not going to bother upgrading all the tests