High-Level Architectural Overview
Relevant source files
The following files were used as context for generating this wiki page:- packages/react/index.js
- packages/react-reconciler/src/ReactFiberReconciler.js
- packages/react-dom/src/client/ReactDOMRoot.js
- packages/scheduler/index.js
- README.md
- packages/react-reconciler/README.md
- packages/react-dom/src/tests/ReactDOMFizzServerBrowser-test.js
- compiler/packages/react-mcp-server/src/index.ts
- packages/react-devtools-inline/README.md
- fixtures/legacy-jsx-runtimes/react-15/cjs/react-jsx-dev-runtime.development.js
React is a JavaScript library for building user interfaces. Its architecture is designed to be declarative, component-based, and flexible, allowing it to be used for developing new features without rewriting existing code. React can render on the server using Node.js and power mobile apps using React Native, demonstrating its "Learn Once, Write Anywhere" philosophy.
The core of React's architecture is centered around a separation of concerns. The main react package contains the logic for defining components and state. The react-reconciler package contains the logic for comparing component trees ("diffing"). Finally, platform-specific renderers like react-dom or react-native use the reconciler to translate the abstract component tree into platform-specific UI elements. This modular design allows React to target different environments.
Sources: README.md:1-17, packages/react-reconciler/README.md:3-9
Core Architectural Packages
The React project is a monorepo composed of several key packages that work together. The following diagram and table illustrate the primary packages and their roles.
This diagram shows the high-level interaction between the core packages.
| Package | Description | Source Files |
|---|---|---|
react | The core package that defines the API for creating components, using hooks, and managing state. It exports functions like createElement, useState, and useEffect. | packages/react/index.js |
react-reconciler | The engine that implements React's reconciliation algorithm (also known as "Fiber"). It determines what changes need to be made to the UI but is platform-agnostic. | packages/react-reconciler/README.md |
react-dom | The renderer for web browsers. It translates the output from the reconciler into DOM operations. It provides APIs like createRoot and hydrateRoot. | packages/react-dom/src/client/ReactDOMRoot.js |
react-dom/server | Provides APIs for server-side rendering (SSR), such as renderToReadableStream, which can generate HTML on the server. | packages/react-dom/src/__tests__/ReactDOMFizzServerBrowser-test.js |
scheduler | A cooperative scheduler used by the reconciler to prioritize and schedule work, enabling features like concurrent rendering. | packages/scheduler/index.js |
react-devtools | A suite of tools for inspecting the React component hierarchy, props, and state. It can be used as a browser extension or embedded in other tools. | packages/react-devtools-inline/README.md |
Sources: README.md, packages/react-reconciler/README.md, packages/react-dom/src/tests/ReactDOMFizzServerBrowser-test.js, packages/react-devtools-inline/README.md
The Rendering Process
React's rendering process begins when an application calls a renderer-specific API to display a component tree in a container. For the DOM, this is typically done using createRoot and root.render().
Client-Side Rendering Flow
The following sequence diagram illustrates the typical client-side rendering and update flow.
This flow shows how react-dom acts as the intermediary between the application code and the reconciler, which in turn uses the DOM-specific "Host Config" to manipulate the actual DOM.
Sources: packages/react-dom/src/client/ReactDOMRoot.js, packages/react-reconciler/README.md:200-205, packages/react-dom/src/tests/ReactRenderDocument-test.js:63-66
Server-Side Rendering (SSR)
React also supports rendering on the server, which is handled by react-dom/server. The primary API for this is renderToReadableStream. This API generates HTML from React components and streams it to the client, which can improve perceived performance.
The server-rendered HTML can then be "hydrated" on the client using ReactDOMClient.hydrateRoot. Hydration attaches event listeners to the existing server-generated markup without re-creating the DOM nodes from scratch. If the client-rendered output does not match the server-rendered HTML, React will warn about a hydration mismatch and attempt to recover by replacing the incorrect server content.
Sources: packages/react-dom/src/tests/ReactDOMFizzServerBrowser-test.js:33-36, packages/react-dom/src/tests/ReactRenderDocument-test.js:180-197
The Reconciler and Host Config
The react-reconciler package is the heart of React. It contains the logic for diffing two component trees to determine the most efficient way to update the UI. It is designed to be platform-agnostic through a "host config" interface.
A host config is an object provided by a renderer (like react-dom or react-native) that tells the reconciler how to interact with the host environment. This includes instructions for creating and updating nodes, handling text, and managing the tree structure.
Sources: packages/react-reconciler/README.md:12-23
Reconciler Modes
The reconciler operates in one of two modes, specified by the host config:
| Mode | supportsMutation | supportsPersistence | Description | Used By |
|---|---|---|---|---|
| Mutation Mode | true | false | The host environment's tree is mutable. The reconciler issues commands like appendChild and removeChild. | react-dom, react-art |
| Persistent Mode | false | true | The host tree is immutable. Every change clones the parent tree and replaces it at the root. | React Native "Fabric" |
Sources: packages/react-reconciler/README.md:73-93
Host Config Interaction
The diagram below shows how the reconciler uses the host config to render to a target.
Key methods defined by the host config include:
createInstance(type, props): Creates a new node for the host environment.createTextInstance(text): Creates a text node.appendChild(parent, child): Appends a child to a parent in mutation mode.commitUpdate(instance, newProps): Updates an existing node.
Sources: packages/react-reconciler/README.md:100-120, 200-205
Development and Tooling
The React ecosystem includes powerful tools for development, debugging, and optimization.
React DevTools
React DevTools allows developers to inspect the component hierarchy, view and edit props and state, and profile performance. The react-devtools-inline package enables embedding DevTools directly into browser-based tools. It works by injecting a global __REACT_DEVTOOLS_GLOBAL_HOOK__ into the target window, which React uses to communicate with the DevTools frontend.
The DevTools architecture consists of a backend that runs with the React application and a frontend that displays the UI. They communicate via a "wall" abstraction, which can be customized for different environments (e.g., postMessage for iframes, WebSockets for Node-to-browser communication).
Sources: packages/react-devtools-inline/README.md:14-25, 140-151
React Compiler (Experimental)
The React Compiler (react-mcp-server) is an experimental tool designed to automatically memoize React components and hooks, reducing the need for manual optimizations like useMemo and useCallback. It analyzes component code and, if it can be safely optimized, returns a compiled version.
The compiler can be invoked as a tool, and it provides diagnostic messages (bailouts) when it cannot optimize a component, guiding the developer to refactor the code to be more compiler-friendly.
The following flowchart shows the basic process of the compile tool.
JSX and Element Creation
JSX is a syntax extension for JavaScript that provides an HTML-like syntax for creating React elements. It is not required to use React, but it is the most common approach. During a build process, JSX is transpiled into standard JavaScript calls to React.createElement or, with newer transforms, jsxDEV (in development).
The jsxDEV function is a development-only function that performs additional validations, such as checking for unique key props in lists and validating propTypes. It ultimately creates a ReactElement object, which is a lightweight description of what to render.
A ReactElement is a plain JavaScript object with properties like:
$$typeof: A unique symbol (REACT_ELEMENT_TYPE) identifying it as a React element.type: The component type (e.g.,'div', or a function/class component).props: The properties passed to the component.key: A special prop used to identify elements in a list.ref: A special prop to get a reference to the underlying instance.
Sources: fixtures/legacy-jsx-runtimes/react-15/cjs/react-jsx-dev-runtime.development.js:463-496, 511-519