概述
刚使用命令 npx create-react-app myapp
生成的 code,console 有以下警告:
ReactDOM.render is no longer supported in React 18. Use createRoot instead.
在 React 18 中,ReactDOM.render
被废弃,当前发出警告,并以兼容模式运行。
index.js
改成下面这样:
import ReactDOM from "react-dom/client";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<App />);
或者下面这样,警告就会消失:
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>);
其他被废弃的方法有:
react-dom: ReactDOM.hydrate
has been deprecated. Using it will warn and run your app in React 17 mode.
react-dom: ReactDOM.unmountComponentAtNode
has been deprecated.
react-dom: ReactDOM.renderSubtreeIntoContainer
has been deprecated.
react-dom/server: ReactDOMServer.renderToNodeStream
has been deprecated.
参考网页:https://dev.to/osmanforhad/react-js-warning-reactdomrender-is-no-longer-supported-in-react-18-use-createroot-instead-until-you-switch-to-the-new-api-1jhh
最后
以上就是不安洋葱为你收集整理的ReactDOM.render is no longer supported in React 18,index.js 修改的全部内容,希望文章能够帮你解决ReactDOM.render is no longer supported in React 18,index.js 修改所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复