react_dom_client__WEBPACK_IMPORTED_MODULE_1__.render 不是 localhost:3000 控制台中显示的函数

Gad*_*Dad 9 reactjs

创建react.js项目后,当我输入任何类型的代码时,它不会显示在本地主机中,因此当我检查并打开控制台时,点击它会显示此错误: Uncaught TypeError: react_dom_client__WEBPACK_IMPORTED_MODULE_1__.render is not a function

at Module../src/index.js (index.js:7:1)
at Module.options.factory (react refresh:6:1)
at __webpack_require__ (bootstrap:24:1)
at startup:7:1
at startup:7:1
Run Code Online (Sandbox Code Playgroud)

小智 12

对于 React 18 中较新的导入方法,上述使用的方法现已被弃用。

\n

您可以使用它来解决问题。

\n
import {StrictMode} from 'react';\nimport {createRoot} from 'react- \ndom/client';\n\nimport App from './App'\n
Run Code Online (Sandbox Code Playgroud)\n

// 这是index.html 文件中div 的ID

\n
const rootElement = \ndocument.getElementById('root');\nconst root = \ncreateRoot(rootElement);\n
Run Code Online (Sandbox Code Playgroud)\n

// \xef\xb8\x8f 如果使用 TypeScript,请添加非空 (!) 断言运算符\n//

\n
const root = createRoot(rootElement!);\n
Run Code Online (Sandbox Code Playgroud)\n

然后

\n
root.render(\n  <StrictMode>\n    <App />\n  </StrictMode>,\n);\n
Run Code Online (Sandbox Code Playgroud)\n