React 18显示ReactDom。Render警告,即使我切换到新标准

发布于 2025-01-25 08:42:13 字数 496 浏览 2 评论 0 原文

我已经切换到React 18,并遵循了如何更改根的渲染方法的官方指南。

这是我的根的渲染代码:

import { createRoot } from 'react-dom/client';

const root = createRoot(document.getElementById('root') as any);
root.render(<App />);

react react-dom ^18.0.0

应用程序正在抛弃:

“在此处输入图像描述”

I have switched to React 18 and followed official guide on how to change root's render method.

Here is my root's render code:

import { createRoot } from 'react-dom/client';

const root = createRoot(document.getElementById('root') as any);
root.render(<App />);

Both react and react-dom are ^18.0.0.

App is throwing this:

enter image description here

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

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

发布评论

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

评论(3

春夜浅 2025-02-01 08:42:13

通过简单地进行重构,下面的代码行还将起作用

从“ react-dom/client”;
import {createroot}
从“ ./components/app”;
导入应用程序
createroot(document.getElementById(“ root”))

By simply refactoring, the line of code below will also work

import { createRoot } from "react-dom/client";
import App from "./components/App";
createRoot(document.getElementById("root")).render(<App tab="home" />);

↘紸啶 2025-02-01 08:42:13

您是否安装了 React React-Dom 软件包的类型定义?您需要添加 packages,然后在 tsconfig.json 文件中设置它们。请记住,包装版本需要兼容。另外, creatoroot 方法的预期参数类型是元素| document fragment 因此,您可以使用感叹号或键入断言喜欢作为元素

index.tsx

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

const container = document.getElementById("root");
const root = createRoot(container!);
root.render(<App />);

tsconfig.json

"types": ["react", "react-dom"]

package.json

"dependencies": {
    "react": "18.0.0",
    "react-dom": "18.0.0",
    "react-scripts": "4.0.3"
  },
"devDependencies": {
    "@types/react": "18.0.0",
    "@types/react-dom": "18.0.0",
    "typescript": "4.4.2"
  },

Have you installed type definitions for react and react-dom packages? You need to add @types/react and @types/react-dom packages and set them in the tsconfig.json file. Keep in mind, package versions needs to be compatible. Also the expected parameter type of the createRoot method is Element | DocumentFragment so you can either use exclamation mark or type assertion like as Element.

index.tsx

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

const container = document.getElementById("root");
const root = createRoot(container!);
root.render(<App />);

tsconfig.json

"types": ["react", "react-dom"]

package.json

"dependencies": {
    "react": "18.0.0",
    "react-dom": "18.0.0",
    "react-scripts": "4.0.3"
  },
"devDependencies": {
    "@types/react": "18.0.0",
    "@types/react-dom": "18.0.0",
    "typescript": "4.4.2"
  },
酷遇一生 2025-02-01 08:42:13

您可以忽略它,

已经合并了一个修复程序,但UNPKG尚未提供包含修复程序的版本。
作者:0stone0

参考:
react@react@endect@instalone in startalone in startalOne in'使用cretotoot

You can ignore it, https://github.com/facebook/react/issues/24292#issuecomment-1091690844

A fix has already been merged, but UNPKG does not yet serve the version which has the fix included.
Author: 0stone0

Reference:
react@18 in "standalone" mode and get a warning using createRoot

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