Review
- 2023/02/04
- 2024-08-20 23:18
[!Summary]
一、Introduction #
createContext
Common APIs #
createPortalflushSyncpreconnectprefetchDNSpreiniteagerly fetch and evaluatepreinitModulefor ESM module.preload预加载;eagerly fetch a resource such as a stylesheet, font, or external script that you expect to use.preloadModulefor ESM module
createPortal #
createPortal is a method on the ReactDOM object. It is used to render a React element into another DOM element outside of the parent component. This is useful for cases like modals, popups, or tooltips where you want the component to break out of its container.
ReactDOM.createPortal(child, container);flushSync detail
#
原来的
forceUpdate已废弃
flushSync lets you force React to flush any updates inside the provided callback synchronously. This ensures that the DOM is updated immediately.
It schedules updates to be performed inside a high-priority task, ensuring that the updates are executed immediately and synchronously before returning control to the caller.
import { flushSync } from 'react-dom';
flushSync(callback);import { flushSync } from 'react-dom';
flushSync(() => {
setSomething(123);
});Caveats #
flushSynccan significantly hurt performance. Use sparingly.flushSyncmay force pending Suspense boundaries to show theirfallbackstate.flushSyncmay run pending Effects and synchronously apply any updates they contain before returning.flushSyncmay flush updates outside the callback when necessary to flush the updates inside the callback. For example, if there are pending updates from a click, React may flush those before flushing the updates inside the callback.
Flushing updates for third-party integrations #
When integrating with third-party code such as browser APIs or UI libraries, it may be necessary to force React to flush updates. Use flushSync to force React to flush any state updates inside the callback synchronously.
This is useful in situations where you need the DOM to be updated immediately, such as for measurements or to ensure synchronous rendering. However, excessive use of flushSync can lead to degraded performance, so it should be used judiciously.
preinit detail
#
preinit lets you eagerly fetch and evaluate a stylesheet or external script.
import { preinit } from 'react-dom';
function AppRoot() {
preinit("https://example.com/style.css", {as: "style", precedence: "medium"});
preinit("https://example.com/script.js", {as: "script"});
return ...;
}If you want the browser to download the script but not to execute it right away, use
preload instead. If you want to load an ESM module, use
preinitModule.
Client APIs #
react-dom/client
createRoothydrateRoot
hydrateRoot detail
#
hydrateRoot lets you display React components inside a browser DOM node whose HTML content was previously generated by react-dom/server.
import { hydrateRoot } from 'react-dom/client';
const root = hydrateRoot(domNode, reactNode, options?)Server APIs #
react-dom/server
renderToNodeStream@Deprecated, userenderToPipeableStreaminsteadrenderToPipeableStreamrenderToReadableStreamrenderToStaticMarkuprenderToStaticNodeStreamrenderToString
renderToString #
The renderToString function in React is part of the react-dom/server package and is used to render React components on the server-side to a static HTML string. It is commonly used for server-side rendering (SSR) in React.