React package for working with the DOM, providing rendering capabilities for both client-side and server-side environments.
npx @tessl/cli install tessl/npm-react-dom@19.2.0React DOM is the official DOM renderer for React, serving as the essential bridge between React's component model and browser DOM manipulation. It provides comprehensive rendering capabilities for client-side, server-side, and static site generation, with specialized APIs for different JavaScript runtimes and deployment targets.
npm install react react-dom// Client-side rendering
import { createRoot, hydrateRoot } from 'react-dom/client';
// Server-side rendering
import { renderToPipeableStream } from 'react-dom/server';
// Static prerendering
import { prerender } from 'react-dom/static';
// Shared utilities
import { createPortal, flushSync } from 'react-dom';For CommonJS:
const { createRoot, hydrateRoot } = require('react-dom/client');
const { renderToPipeableStream } = require('react-dom/server');
const { createPortal, flushSync } = require('react-dom');import { createRoot } from 'react-dom/client';
function App() {
return <div>Hello World</div>;
}
const root = createRoot(document.getElementById('root'));
root.render(<App />);import { renderToPipeableStream } from 'react-dom/server';
function App() {
return <div>Hello World</div>;
}
function handleRequest(req, res) {
const stream = renderToPipeableStream(<App />, {
onShellReady() {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
stream.pipe(res);
},
onError(error) {
console.error(error);
res.statusCode = 500;
res.end('Internal Server Error');
}
});
}import { hydrateRoot } from 'react-dom/client';
function App() {
return <div>Hello World</div>;
}
hydrateRoot(document.getElementById('root'), <App />);React DOM is organized into several functional areas:
createRoot and hydrateRoot APIsreact-dom - Shared utilities and resource hintsreact-dom/client - Client-side renderingreact-dom/server - Server-side rendering (auto-selects runtime variant)react-dom/server.node - Node.js specific SSRreact-dom/server.browser - Browser/Deno specific SSRreact-dom/server.edge - Edge runtime specific SSRreact-dom/server.bun - Bun specific SSRreact-dom/static - Static prerendering (auto-selects runtime variant)react-dom/static.node - Node.js specific static generationreact-dom/static.browser - Browser specific static generationreact-dom/static.edge - Edge runtime specific static generationreact-dom/profiling - Profiling build with React DevTools integrationreact-dom/test-utils - Testing utilities (deprecated)APIs for rendering React components in the browser with modern concurrent rendering features.
function createRoot(
container: Element | DocumentFragment,
options?: CreateRootOptions
): Root;
interface Root {
render(children: ReactNode): void;
unmount(): void;
}
interface CreateRootOptions {
unstable_strictMode?: boolean;
identifierPrefix?: string;
onUncaughtError?: (error: Error, errorInfo: ErrorInfo) => void;
onCaughtError?: (error: Error, errorInfo: ErrorInfo) => void;
onRecoverableError?: (error: Error, errorInfo: ErrorInfo) => void;
onDefaultTransitionIndicator?: () => void | (() => void);
unstable_transitionCallbacks?: TransitionTracingCallbacks;
}
function hydrateRoot(
container: Element | Document,
initialChildren: ReactNode,
options?: HydrateRootOptions
): Root;
interface HydrateRootOptions extends CreateRootOptions {
onHydrated?: (suspenseBoundary: SuspenseBoundary) => void;
onDeleted?: (suspenseBoundary: SuspenseBoundary) => void;
formState?: ReactFormState;
}Streaming server-side rendering APIs optimized for different JavaScript runtimes.
// Node.js - Primary streaming API
function renderToPipeableStream(
children: ReactNode,
options?: RenderToPipeableStreamOptions
): PipeableStream;
interface PipeableStream {
pipe<T extends Writable>(destination: T): T;
abort(reason?: string): void;
}
// Web Streams API (Browser/Edge/Bun)
function renderToReadableStream(
children: ReactNode,
options?: RenderToReadableStreamOptions
): Promise<ReadableStream & { allReady: Promise<void> }>;
// Resume from postponed state
function resumeToPipeableStream(
children: ReactNode,
postponedState: PostponedState,
options?: RenderToPipeableStreamOptions
): PipeableStream;
function resume(
children: ReactNode,
postponedState: PostponedState,
options?: RenderToReadableStreamOptions
): Promise<ReadableStream>;APIs for prerendering React components to static HTML with support for postponed content.
// Web Streams API
function prerender(
children: ReactNode,
options?: PrerenderOptions
): Promise<{ prelude: ReadableStream; postponed: PostponedState | null }>;
// Node.js Streams API
function prerenderToNodeStream(
children: ReactNode,
options?: PrerenderOptions
): Promise<{ prelude: Readable; postponed: PostponedState | null }>;
// Resume and prerender with postponed state
function resumeAndPrerender(
children: ReactNode,
postponedState: PostponedState,
options?: PrerenderOptions
): Promise<{ prelude: ReadableStream; postponed: PostponedState | null }>;
function resumeAndPrerenderToNodeStream(
children: ReactNode,
postponedState: PostponedState,
options?: PrerenderOptions
): Promise<{ prelude: Readable; postponed: PostponedState | null }>;Render React children into a DOM node outside the current component hierarchy, useful for modals, tooltips, and overlays.
function createPortal(
children: ReactNode,
container: Element | DocumentFragment,
key?: string
): ReactPortal;Usage Example:
import { createPortal } from 'react-dom';
function Modal({ children }) {
return createPortal(
<div className="modal">{children}</div>,
document.getElementById('modal-root')
);
}APIs for optimizing resource loading with DNS prefetch, preconnect, preload, and preinit capabilities.
function prefetchDNS(href: string): void;
function preconnect(
href: string,
options?: { crossOrigin?: 'anonymous' | 'use-credentials' }
): void;
function preload(href: string, options: PreloadOptions): void;
interface PreloadOptions {
as: string; // Required: 'style' | 'font' | 'script' | 'image' | etc.
crossOrigin?: 'anonymous' | 'use-credentials';
integrity?: string;
type?: string;
media?: string;
nonce?: string;
fetchPriority?: 'high' | 'low' | 'auto';
imageSrcSet?: string;
imageSizes?: string;
referrerPolicy?: string;
}
function preloadModule(
href: string,
options?: PreloadModuleOptions
): void;
interface PreloadModuleOptions {
as?: string;
crossOrigin?: 'anonymous' | 'use-credentials';
integrity?: string;
nonce?: string;
}
function preinit(href: string, options: PreinitOptions): void;
interface PreinitOptions {
as: 'style' | 'script'; // Required
precedence?: string;
crossOrigin?: 'anonymous' | 'use-credentials';
integrity?: string;
nonce?: string;
fetchPriority?: 'high' | 'low' | 'auto';
}
function preinitModule(
href: string,
options?: PreinitModuleOptions
): void;
interface PreinitModuleOptions {
as?: string;
crossOrigin?: 'anonymous' | 'use-credentials';
integrity?: string;
nonce?: string;
}React hooks for progressive enhancement and Server Actions integration.
function useFormState<S, P = FormData>(
action: (prevState: Awaited<S>, payload: P) => S,
initialState: Awaited<S>,
permalink?: string
): [state: Awaited<S>, dispatch: (payload: P) => void, isPending: boolean];
// Return type is a discriminated union based on pending state
type FormStatusNotPending = {
pending: false;
data: null;
method: null;
action: null;
};
type FormStatusPending = {
pending: true;
data: FormData;
method: string;
action: string | ((formData: FormData) => void | Promise<void>) | null;
};
type FormStatus = FormStatusPending | FormStatusNotPending;
function useFormStatus(): FormStatus;
function requestFormReset(form: HTMLFormElement): void;Control over React's update scheduling and performance profiling.
function flushSync<R>(fn?: () => R): R;Usage Example:
import { flushSync } from 'react-dom';
// Force synchronous update (use sparingly)
flushSync(() => {
setState(newValue);
});
// DOM is guaranteed to be updated hereWarning: flushSync can harm performance. Use only when you need to read from the DOM immediately after a state update.
const version: string; // "19.2.0"Exported from all entry points.
interface ErrorInfo {
componentStack?: string;
errorBoundary?: React.Component;
}interface ReactFormState<S = any, P = any> {
[key: string]: any;
}// Opaque type representing postponed rendering state
type PostponedState = OpaquePostponedState;Used for resuming rendering in static generation and server rendering scenarios.
// Opaque type representing a Suspense boundary node
type SuspenseBoundary = Comment;function unstable_batchedUpdates<A, R>(fn: (a: A) => R, a: A): R;Status: Deprecated - batching is now automatic in React 18+. This is now a no-op passthrough.
function renderToString(children: ReactNode): string;
function renderToStaticMarkup(children: ReactNode): string;Status: Deprecated - use streaming APIs (renderToPipeableStream or renderToReadableStream) instead. These legacy APIs don't support Suspense or streaming.
function act(callback: () => void | Promise<void>): Promise<void>;Status: Deprecated - import act from 'react' package instead of 'react-dom/test-utils'.
Warning: This is an internal API exported from the main entry point. Do not use - it will break in future React versions without warning.
React DOM provides runtime-specific builds optimized for different JavaScript environments:
renderToPipeableStream)renderToReadableStream)renderToReadableStream)renderToReadableStream)prerenderToNodeStream)prerender with ReadableStream)prerender with ReadableStream)createRoot over legacy ReactDOM.renderrenderToPipeableStream or renderToReadableStream over deprecated renderToStringflushSync when absolutely necessary as it can harm performanceprefetchDNS, preconnect, preload, and preinit to optimize loadinguseFormState, useFormStatus) for better UXcreateRoot options for better error handling// Old (React 17)
import ReactDOM from 'react-dom';
ReactDOM.render(<App />, document.getElementById('root'));
// New (React 18+)
import { createRoot } from 'react-dom/client';
const root = createRoot(document.getElementById('root'));
root.render(<App />);// Old (deprecated)
import { renderToString } from 'react-dom/server';
const html = renderToString(<App />);
// New (streaming)
import { renderToPipeableStream } from 'react-dom/server';
const stream = renderToPipeableStream(<App />, {
onShellReady() {
stream.pipe(res);
}
});