Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

升级 React


升级到 React 18

将现有的 React 应用程序升级到 18 版本只需要两个步骤。

如果您已经在使用最新版本的 create-react-app(它使用 React 18 版本),则可以跳过此部分。


步骤 1:安装 React 18

要安装最新版本,请在项目文件夹中从终端运行以下命令:

npm i react@latest react-dom@latest

步骤 2:使用新的根 API

为了利用 React 18 的并发特性,您需要为客户端渲染使用新的根 API。

// Before
import ReactDOM from 'react-dom';
ReactDOM.render(<App />, document.getElementById('root'));

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

您的应用程序在不使用新的根 API 的情况下也可以工作。如果您继续使用 ReactDOM.render,您的应用程序的行为将与 React 17 相同。


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.