在我的index.js文件中有什么错误,这会导致我这个错误react-dom.development.js?

发布于 2025-01-26 15:45:36 字数 1238 浏览 4 评论 0 原文

当我检查控制台时,我会得到:

react-dom.development.js:86 Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot

我正在使用“ react-dom”:“^18.0.0” “ react-router-dom”:“^6.3.0” < /代码>。但是,当我在 index.js 中使用 createroot 时,我没有错误,但是某些机构不起作用(例如,我得到 here

我尝试了什么:

import "./index.css";
import App from "./App";
import React from "react";
import ReactDOM from "react-dom";

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById("root")
);

我尝试过:

import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App";

const rootElement = document.getElementById("root");
const root = createRoot(rootElement);

root.render(
  <StrictMode>
    <App />
  </StrictMode>
);

when I check the console I get :

react-dom.development.js:86 Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot

I'm using "react-dom": "^18.0.0", "react-router-dom": "^6.3.0". But then when I use createRoot in my index.js I don't have the error but some functionnalities are not working (such as this issue that I get here)

What I have:

import "./index.css";
import App from "./App";
import React from "react";
import ReactDOM from "react-dom";

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById("root")
);

What I tried :

import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App";

const rootElement = document.getElementById("root");
const root = createRoot(rootElement);

root.render(
  <StrictMode>
    <App />
  </StrictMode>
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

治碍 2025-02-02 15:45:36
import ReactDOM from "react-dom/client";
const root = ReactDOM.createRoot(document.getElementbyId("root")
root.render(<App/>);

您从不进口反应。

import ReactDOM from "react-dom/client";
const root = ReactDOM.createRoot(document.getElementbyId("root")
root.render(<App/>);

you never imported reactDOM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文