react-dom
包包含仅支持 Web 应用(在浏览器 DOM 环境中运行)的方法。React Native 不支持它们。
¥The react-dom
package contains methods that are only supported for the web applications (which run in the browser DOM environment). They are not supported for React Native.
API
这些 API 可以从你的组件中导入。它们很少被使用:
¥These APIs can be imported from your components. They are rarely used:
-
createPortal
允许你在 DOM 树的不同部分渲染子组件。¥
createPortal
lets you render child components in a different part of the DOM tree. -
flushSync
允许你强制 React 刷新状态更新并同步更新 DOM。¥
flushSync
lets you force React to flush a state update and update the DOM synchronously.
资源预加载 API
¥Resource Preloading APIs
这些 API 可用于在你知道需要时立即预加载脚本、样式表和字体等资源,例如在导航到将使用资源的另一个页面之前,从而使应用运行得更快。
¥These APIs can be used to make apps faster by pre-loading resources such as scripts, stylesheets, and fonts as soon as you know you need them, for example before navigating to another page where the resources will be used.
基于 React 的框架 经常为你处理资源加载,因此你可能不必自己调用这些 API。有关详细信息,请参阅你的框架的文档。
¥React-based frameworks frequently handle resource loading for you, so you might not have to call these APIs yourself. Consult your framework’s documentation for details.
-
prefetchDNS
允许你预取你希望连接到的 DNS 域名的 IP 地址。¥
prefetchDNS
lets you prefetch the IP address of a DNS domain name that you expect to connect to. -
preconnect
允许你连接到你希望从中请求资源的服务器,即使你还不知道需要什么资源。¥
preconnect
lets you connect to a server you expect to request resources from, even if you don’t know what resources you’ll need yet. -
preload
允许你获取你希望使用的样式表、字体、图片或外部脚本。¥
preload
lets you fetch a stylesheet, font, image, or external script that you expect to use. -
preloadModule
允许你获取你希望使用的 ESM 模块。¥
preloadModule
lets you fetch an ESM module that you expect to use. -
preinit
允许你获取并评估外部脚本或获取并插入样式表。¥
preinit
lets you fetch and evaluate an external script or fetch and insert a stylesheet. -
preinitModule
允许你获取并评估 ESM 模块。¥
preinitModule
lets you fetch and evaluate an ESM module.
入口点
¥Entry points
react-dom
包提供了两个额外的入口点:
¥The react-dom
package provides two additional entry points:
-
react-dom/client
包含在客户端(在浏览器中)渲染 React 组件的 API。¥
react-dom/client
contains APIs to render React components on the client (in the browser). -
react-dom/server
包含用于在服务器上渲染 React 组件的 API。¥
react-dom/server
contains APIs to render React components on the server.
弃用的 API
¥Deprecated APIs
-
findDOMNode
找到与类组件实例对应的最近的 DOM 节点。¥
findDOMNode
finds the closest DOM node corresponding to a class component instance. -
hydrate
将树挂载到从服务器 HTML 创建的 DOM 中。已弃用,取而代之的是hydrateRoot
。¥
hydrate
mounts a tree into the DOM created from server HTML. Deprecated in favor ofhydrateRoot
. -
render
将树挂载到 DOM 中。已弃用,取而代之的是createRoot
。¥
render
mounts a tree into the DOM. Deprecated in favor ofcreateRoot
. -
unmountComponentAtNode
从 DOM 中卸载树。已弃用,取而代之的是root.unmount()
。¥
unmountComponentAtNode
unmounts a tree from the DOM. Deprecated in favor ofroot.unmount()
.