Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findDOMNode is deprecated and will be removed in the next major release. #8752

Open
yuzh0816 opened this issue Sep 30, 2024 · 12 comments
Open

Comments

@yuzh0816
Copy link

Reproduction link

https://github.com/yuzh0816/antd-test

Steps to reproduce

执行npm run dev后,打开控制台即可看到报错

What is expected?

希望这处报错被解决。

What is actually happening?

报错:
Warning: findDOMNode is deprecated and will be removed in the next major release. Instead, add a ref directly to the element you want to reference.

Environment Info
antd 5.21.1
React 18.3.1
System Windows Server 2022
Browser 所有浏览器
Copy link

在 Ant Design 的 GitHub 仓库中,确实有一些与 findDOMNode 被弃用相关的问题和 PR。以下是一些相关的链接:

  1. Issue ant-design/ant-design#243 - 什么时候支持React 0.14.x 来着?
  2. Issue ant-design/ant-design#400 - react-dom支持
  3. Issue ant-design/ant-design#13277 - Access the inner dom of components
  4. PR ant-design/ant-design#18555 - feat(Input): add input ref prop
  5. Issue ant-design/ant-design#22477 - Warning: findDOMNode is deprecated in StrictMode.

你可以参考这些问题和 PR,看看是否有解决方案适用于你的情况。

通常情况下,解决这个问题的方法是避免使用 findDOMNode,而是直接在你需要引用的元素上使用 ref。如果你能提供具体的代码片段,我可以帮助你进行修改。

如果你有进一步的问题,请在评论中描述并 @petercat-assistant 以开始与我对话。

@afc163
Copy link
Member

afc163 commented Sep 30, 2024

如何重现?我这里没有看到警告信息。

image

Copy link

Hello @yuzh0816. Please provide a online reproduction by forking this one or provide a minimal GitHub repository like create-react-app-antd. Issues labeled by Need Reproduce will be closed if no activities in 3 days.

你好 @yuzh0816,我们需要你提供一个在线的重现实例以便于我们帮你排查问题。你可以通过 fork 这个在线重现案例 ,或者提供一个最小化的 GitHub 仓库(类似 create-react-app-antd)。3 天内未跟进此 issue 将会被自动关闭。

什么是最小化重现,为什么这是必需的?

@yuzh0816
Copy link
Author

如何重现?我这里没有看到警告信息。

image

问题刚刚排查出来,是react版本的问题,似乎为18.2.0时不会报错。

@yuzh0816
Copy link
Author

如何重现?我这里没有看到警告信息。

image

版本一旦变为18.3.1后就会报错。

@yuzh0816 yuzh0816 reopened this Sep 30, 2024
Copy link

目前没有找到与 findDOMNode 相关的具体解决方案。为了帮助你解决这个问题,我建议你在代码中查找使用 findDOMNode 的地方,并将其替换为直接使用 ref

以下是一个简单的示例,展示了如何将 findDOMNode 替换为 ref

之前的代码:

import ReactDOM from 'react-dom';

class MyComponent extends React.Component {
  componentDidMount() {
    const node = ReactDOM.findDOMNode(this);
    // 你的逻辑
  }

  render() {
    return <div>My Component</div>;
  }
}

修改后的代码:

import React from 'react';

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
  }

  componentDidMount() {
    const node = this.myRef.current;
    // 你的逻辑
  }

  render() {
    return <div ref={this.myRef}>My Component</div>;
  }
}

你可以在你的项目中查找所有使用 findDOMNode 的地方,并按照上述方式进行修改。

如果你在修改过程中遇到任何问题,请随时在评论中描述你的问题并 @petercat-assistant 与我进行对话。

For further assistance, please describe your question in the comments and @petercat-assistant to start a conversation with me.

@yuzh0816
Copy link
Author

如何重现?我这里没有看到警告信息。

image

麻烦您重新测试下刚刚的仓库,我更新了下package信息,将react版本切换至了18.3.1,问题即可复现。

@afc163 afc163 transferred this issue from ant-design/ant-design Oct 1, 2024
@afc163
Copy link
Member

afc163 commented Oct 1, 2024

看上去是 ProLayout 的实现问题。

Copy link

github-actions bot commented Oct 1, 2024

以下的 Issues 可能会帮助到你 / The following issues may help you

@afc163
Copy link
Member

afc163 commented Oct 1, 2024

@researchlab
Copy link

使用 ProTable 也报这个错误

版本信息
"dependencies": {
"@ant-design/icons": "^5.4.0",
"@ant-design/pro-components": "^2.7.15",
"@apollo/client": "^3.11.4",
"@types/lodash": "^4.17.7",
"antd": "5.20.3",
"antd-img-crop": "^4.23.0",
"graphql": "^16.9.0",
"lodash": "^4.17.21",
"pro-components": "link: @ant-design/pro-components",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "6.6.1",
"vite-plugin-eslint": "^1.8.1"
},

使用方式

import { ActionType, PageContainer, ProTable } from '@ant-design/pro-components';

<ProTable /> 

注释掉 就不报错

浏览器报错信息
Warning: 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 Error Component Stack
at SingleObserver (index.js:10:24)
at ResizeObserver2 (index.js:12:24)
at index.js:33:34
at Tooltip2 (Tooltip.js:11:32)
at index.js:30:16
at span ()
at div ()
at div ()
at div ()
at div ()
at div ()
at DomWrapper3 (DomWrapper.js:13:5)
at SingleObserver (index.js:10:24)
at ResizeObserver2 (index.js:12:24)
at ListToolBar2 (index.js:93:34)
at ToolBar (index.js:91:27)
at ToolbarRender2 (index.js:186:5)
at div ()
at div ()
at index.js:23:25
at div ()
at TableRender (Table.js:37:22)
at ProTable2 (Table.js:295:28)
at ErrorBoundary2 (index.js:17:5)
at ProConfigProvider2 (index.js:242:24)
at Container2 (Provide.js:199:34)
at ProviderTableContainer2 (Table.js:806:21)
at div ()
at div ()
at div ()
at GridContent2 (index.js:15:15)
at div ()
at PageContainerBase2 (index.js:172:24)
at ProConfigProvider2 (index.js:242:24)
at PageContainer2 ()
at Product (index.tsx:16:18)
at RenderedRoute (hooks.tsx:566:26)
at div ()
at main ()
at layout.js:39:18
at Content ()
at ErrorBoundary2 (index.js:17:5)
at WrapContent2 (WrapContent.js:9:21)
at div ()
at div ()
at layout.js:59:13
at Layout ()
at div ()
at BaseProLayout2 (ProLayout.js:171:13)
at MotionWrapper (MotionWrapper.js:8:5)
at ProviderChildren (index.js:105:5)
at ConfigProvider (index.js:394:25)
at SWRConfig (index.mjs:529:13)
at ConfigProviderContainer2 (index.js:115:24)
at LocaleProvider (index.js:11:5)
at MotionWrapper (MotionWrapper.js:8:5)
at ProviderChildren (index.js:105:5)
at ConfigProvider (index.js:394:25)
at ProConfigProvider2 (index.js:242:24)
at MotionWrapper (MotionWrapper.js:8:5)
at ProviderChildren (index.js:105:5)
at ConfigProvider (index.js:394:25)
at ProLayout2 (ProLayout.js:507:28)
at Layout (index.tsx:37:20)
at RenderedRoute (hooks.tsx:566:26)
at Routes (components.tsx:373:3)
at div ()
at div ()
at div ()
at Spin (index.js:29:18)
at UserInfo (index.tsx:10:21)
at contextFactory.tsx:19:15
at Router (components.tsx:291:13)
at BrowserRouter (index.tsx:285:3)
at ApolloProvider (ApolloProvider.tsx:15:9)

解决方案之一
看了上述讨论, 将react版本降到18.2.0 就不报错了

"react": "18.2.0",
"react-dom": "18.2.0",

@resetsix
Copy link

see #8837

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants