facebook-react

IndexedCommit: 230772f0 pullsUpdated Jan 29, 2026

React is a JavaScript library for building user interfaces that lets you create interactive UIs in a declarative way through components. It supports both client-side rendering in browsers and server-s

Install this reference
Reference

React

React is a JavaScript library for building user interfaces that lets you create interactive UIs in a declarative way through components. It supports both client-side rendering in browsers and server-side rendering for better performance and SEO.

Quick References

FilePurpose
packages/react/index.jsMain React package entry point with core APIs
packages/react-dom/index.jsReactDOM entry point for DOM utilities
packages/react-dom/src/client/ReactDOMClient.jscreateRoot and hydrateRoot APIs
README.mdProject overview and documentation links

Packages

Packagenpm nameDescription
packages/reactreactCore React library with components and hooks
packages/react-domreact-domDOM renderer for web browsers
packages/react-dom/clientreact-dom/clientClient-side rendering APIs (createRoot, hydrateRoot)
packages/react-dom/serverreact-dom/serverServer-side rendering APIs (renderToPipeableStream, renderToReadableStream)
packages/react-test-rendererreact-test-rendererDeprecated snapshot testing renderer
packages/react-isreact-isRuntime type checking for React elements
packages/use-sync-external-storeuse-sync-external-storeBackwards-compatible shim for useSyncExternalStore hook
packages/react-refreshreact-refreshFast Refresh integration for bundlers
packages/schedulerschedulerCooperative scheduling (internal React dependency)

When to Use

  • Building modern single-page applications with dynamic, interactive user interfaces
  • Creating reusable component libraries for complex UIs with state management needs
  • Implementing server-side rendering for improved performance and SEO in web applications
  • Developing applications that need both client and server-side rendering (isomorphic/universal)
  • Building progressive web applications with optimistic UI updates

Installation

# Core React and DOM renderer
npm install react react-dom

# Additional utilities as needed
npm install react-is
npm install use-sync-external-store

Best Practices

  1. Use functional components with hooks - Prefer modern functional components and hooks over class components for better code organization and reusability
  2. Keep components pure - Components should return consistent output for the same props and state to ensure predictable behavior
  3. Minimize state updates - Batch related state updates together and use useCallback/useMemo to prevent unnecessary re-renders
  4. Use production builds - Always use the production build when deploying to get smaller bundle size and better performance
  5. Prefer declarative updates - Use useActionState for form actions and declarative state management over imperative approaches

Common Patterns

Basic Component with State:

import { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  return (
    <div>
      <h1>{count}</h1>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

Mounting an Application:

import { createRoot } from 'react-dom/client';

function App() {
  return <div>Hello World</div>;
}

const root = createRoot(document.getElementById('root'));
root.render(<App />);

Server-Side Rendering:

import { renderToPipeableStream } from 'react-dom/server';

function App() {
  return <div>Hello World</div>;
}

// Node.js server
const stream = renderToPipeableStream(<App />, {
  onShellReady() {
    response.statusCode = 200;
    response.setHeader('Content-type', 'text/html');
    stream.pipe(response);
  },
});

Hydrating Existing HTML:

import { hydrateRoot } from 'react-dom/client';

hydrateRoot(document.getElementById('root'), <App />);

Type Checking with react-is:

import * as ReactIs from 'react-is';

ReactIs.isElement(<div />); // true
ReactIs.isFragment(<></>); // true
ReactIs.isPortal(portal); // true

Custom Hooks with External Stores:

import { useSyncExternalStore } from 'react';

function useWindowWidth() {
  return useSyncExternalStore(
    subscribe => {
      window.addEventListener('resize', subscribe);
      return () => window.removeEventListener('resize', subscribe);
    },
    () => window.innerWidth
  );
}

API Quick Reference

Core React

ExportTypeDescription
Component, PureComponentClassBase classes for class components
FragmentComponentGroup multiple elements without extra DOM nodes
StrictModeComponentDevelopment-only tool for detecting potential issues
SuspenseComponentDisplay fallback while waiting for async operations
createContextFunctionCreates context for passing data through component tree
createElementFunctionProgrammatic creation of React elements
forwardRefHOCForward refs to child components
lazyFunctionLazy load component code
memoHOCMemoize component to prevent unnecessary re-renders
useStateHookAdd state to functional components
useEffectHookPerform side effects in functional components
useRefHookPersist values across renders without causing re-renders
useCallbackHookMemoize callback functions
useMemoHookMemoize expensive calculations
useContextHookRead context values
useReducerHookManage complex state with reducer pattern
useTransitionHookMark state updates as transitions
startTransitionFunctionStart a transition for non-urgent updates
useDeferredValueHookDefer updates to less critical values
useIdHookGenerate unique IDs with consistent server/client value
useActionStateHookManage form action state
useOptimisticHookOptimistically update UI before server confirmation
useEffectEventHookCreate functions that don't change between renders
useInsertionEffectHookInsert styles before DOM mutations
useImperativeHandleHookExpose imperative methods to parent components
cacheFunctionCache computed values
cacheSignalFunctionSignal for cache invalidation

ReactDOM Client

ExportTypeDescription
createRootFunctionCreate a root for concurrent rendering
hydrateRootFunctionHydrate server-rendered HTML

ReactDOM

ExportTypeDescription
createPortalFunctionRender children into a different DOM node
flushSyncFunctionForce synchronous execution of updates
prefetchDNSFunctionPrefetch DNS resolution
preconnectFunctionPreconnect to origins
preloadFunctionPreload resources
preloadModuleFunctionPreload JavaScript modules
preinitFunctionPreinit stylesheets and scripts
preinitModuleFunctionPreinit JavaScript modules with inline execution
unstable_batchedUpdatesFunctionBatch multiple updates together (no-op, kept for compatibility)
useFormStatusHookAccess form submission status
useFormStateHookManage form state based on server actions

ReactDOM Server

ExportTypeDescription
renderToPipeableStreamFunctionRender to a Node.js writable stream
renderToReadableStreamFunctionRender to a Web Stream
renderToStringFunctionRender to string (legacy)
renderToStaticMarkupFunctionRender to static HTML without data attributes

react-is

ExportTypeDescription
isValidElementTypeFunctionCheck if a value is a valid React element type
typeOfFunctionGet the type of a React element
isElementFunctionCheck if value is a React element
isFragmentFunctionCheck if value is a Fragment
isPortalFunctionCheck if value is a Portal
isMemoFunctionCheck if value is a memoized component
isLazyFunctionCheck if value is a lazy component
isForwardRefFunctionCheck if value is a forwardRef component
isSuspenseFunctionCheck if value is a Suspense boundary

use-sync-external-store

ExportTypeDescription
useSyncExternalStoreHookSubscribe to external store data (polyfill for React < 18)

“Explore distant worlds.”

© 2026 Oscar Gabriel