pro-components icon indicating copy to clipboard operation
pro-components copied to clipboard

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

Open yuzh0816 opened this issue 1 year ago • 11 comments

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 所有浏览器

yuzh0816 avatar Sep 30 '24 07:09 yuzh0816

在 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 以开始与我对话。

petercat-assistant[bot] avatar Sep 30 '24 07:09 petercat-assistant[bot]

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

image

afc163 avatar Sep 30 '24 07:09 afc163

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 将会被自动关闭。

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

github-actions[bot] avatar Sep 30 '24 07:09 github-actions[bot]

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

image

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

yuzh0816 avatar Sep 30 '24 07:09 yuzh0816

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

image

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

yuzh0816 avatar Sep 30 '24 07:09 yuzh0816

目前没有找到与 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.

petercat-assistant[bot] avatar Sep 30 '24 07:09 petercat-assistant[bot]

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

image

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

yuzh0816 avatar Sep 30 '24 07:09 yuzh0816

看上去是 ProLayout 的实现问题。

afc163 avatar Oct 01 '24 14:10 afc163

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

  • [#8685][🧐[问题] QueryFilter组件 findDOMNode is deprecated and will be removed in the next major release][83%]
  • [#8686][🐛[BUG] pro-layout 报警告 findDOMNode is deprecated and will be removed in the next major release.][70%]

github-actions[bot] avatar Oct 01 '24 14:10 github-actions[bot]

有不少类似问题:https://github.com/ant-design/pro-components/issues?q=sort%3Aupdated-desc+is%3Aissue+is%3Aopen+findDOMNode

afc163 avatar Oct 01 '24 14:10 afc163

使用 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 /> 

注释掉<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",

researchlab avatar Oct 16 '24 02:10 researchlab

see https://github.com/ant-design/pro-components/discussions/8837

resetsix avatar Oct 29 '24 11:10 resetsix

升级到react 19,ant升级到最新版本,即可解决。

leshalv avatar Feb 18 '25 01:02 leshalv