Introducing a new way to review your PRs.

Read the announcement

reflex-dev / reflex

🕸️ Web apps in pure Python 🐍

View on GitHub

30 documents · Updated Nov 23, 2026

docs / component-system

Component system

The component system turns Python UI declarations into a React-facing component tree. It keeps the tree, its props, its state expressions, its events, its styles, and its package needs together so the frontend compiler can produce a working page.

Component tree

The tree is the main shared model between Python application code and the frontend compiler. Each node describes one React element or component and can contain child nodes or component values inside its props.

Python declaration model

The Component abstraction is defined in packages/…/component.py. A component class describes the React library and tag that it represents, then declares typed fields for values that callers may set. Application code creates instances with Component.create(...) and passes children as positional values.

The base model also carries framework data that does not become a normal React prop. This data includes event handlers, styles, ids, refs, keys, aliases, and rules for valid parent and child relationships. This split lets Python validate a declaration before the compiler sends its React meaning to the browser.

References: packages/…/component.py• L336-365 packages/…/component.py• L776-845 packages/…/component.py• L1293-1342

React-facing render model

Each component resolves to a tag record with a name, a list of formatted props, and rendered children. Python names become React names such as className or onClick, while values become literals or JavaScript expressions. A component can use a library export as its tag, a browser element as a global tag, or an alias when the exported name would conflict with another component.

The model also supports fragments and bare values. A string or other supported primitive becomes a child node without requiring application code to create a wrapper element. A tuple becomes a fragment so a function can return several sibling nodes.

References: packages/…/tag.py• L34-105 packages/…/component.py• L1182-1240 packages/…/bare.py• L57-80 packages/…/fragment.py• L6-13

Declaration lifecycle

The component lifecycle moves a declaration from Python values to a complete frontend unit. The same tree supplies both what React renders and the extra resources needed to make that render work.

Child and prop normalization

Component creation first checks child types and component-specific nesting rules. It then normalizes primitive children and tuples, removes absent prop values, validates typed reactive values, converts event handlers into event chains, and folds style and accessibility attributes into the component model.

This early normalization gives later stages one stable shape. It also makes invalid component use fail near the Python declaration instead of producing unclear browser output.

References: packages/…/component.py• L1280-1342 packages/…/component.py• L1578-1645 packages/…/component.py• L983-1137

Render and dependency collection

Rendering walks the component tree and creates the React-facing tag record. Dependency collection walks the same tree, including components stored in props or carried by a reactive value, to gather imports, dynamic imports, hooks, custom JavaScript, refs, and app-level wrapper providers.

This relationship is important: a component is not complete when its tag and props are known. A state expression may require an import or hook, and an event may require providers at the app root. The component system carries those needs with the declaration so the compiler can assemble them without a separate dependency map.

References: packages/…/component.py• L1262-1278 packages/…/component.py• L1478-1496 packages/…/component.py• L1824-1996 packages/…/component.py• L2137-2289

Frontend output

The compiler turns the render record into JSX calls and places the collected resources around the page component. It emits imports first, then dynamic imports and custom code, then hooks, and finally the rendered tree. The output is a React component that can use state expressions and dispatch events while it renders.

The compiler also keeps generated component modules aligned with the current compile. A real compile removes memo modules that are no longer emitted, while a dry run leaves generated output unchanged.

The high-level path from a Python declaration to browser-facing code is:

flowchart LR
    A[Python UI declaration] --> B[Component tree]
    B --> C[Typed props and event chains]
    B --> D[Render records]
    B --> E[Imports hooks and providers]
    C --> D
    D --> F[React page or component module]
    E --> F
    F --> G[Browser UI]

References: reflex/…/compiler.py• L243-264 reflex/…/compiler.py• L1491-1514 reflex/…/utils.py• L1048-1135 packages/…/templates.py• L465-513 packages/…/templates.py• L708-745

Component contracts

Component contracts define how a Python declaration may interact with React and with the rest of the Reflex runtime. They are the boundaries that component authors and package maintainers must keep stable.

Props and reactive values

A prop is a value accepted by a component and sent to its React tag. A Var is a reactive value: it represents a JavaScript expression rather than only a Python value. The component system validates the type of a Var against the prop contract, then preserves the expression and its dependency data for frontend compilation.

Nested prop objects use PropsBase when a React prop needs a structured JavaScript object. That object keeps Python field names readable while serializing nested keys in the camel-case form expected by React libraries.

See Reactive variables for the wider state and expression model.

References: packages/…/component.py• L1014-1051 packages/…/props.py• L230-355 packages/…/tag.py• L83-105

Events and lifecycle hooks

An event trigger connects a browser action to a backend event handler or a JavaScript expression. The component declares the trigger and its argument shape; creation turns the supplied handler into an event chain. Rendering exposes ordinary triggers as React props, while mount and unmount triggers become lifecycle behavior.

Components can also contribute React hooks and page-level JavaScript. The framework merges these contributions across the tree and removes duplicates. State and event dependencies add the providers needed for dispatch at the app level.

References: packages/…/component.py• L703-770 packages/…/component.py• L1138-1154 packages/…/component.py• L1998-2175

Styles and themes

Styles are part of the component model, not a separate pass over raw JSX. The system combines component defaults, app-level component styles, and instance styles before it emits the CSS prop. Later values override earlier values, and style reactive values keep their dependency data.

Theme components can add a provider around the application. This allows a package to make shared visual settings available to its components while keeping the Python declaration as the place where the relationship is expressed.

See Styling and theming for the style and theme model.

References: packages/…/component.py• L1363-1461 packages/…/base.py• L96-134 packages/…/base.py• L166-248

Libraries and runtime dependencies

Each wrapped React component names its library and exported tag. The component can also declare libraries needed for installation, add imports for special behavior, or request a dynamic import when server rendering is not safe. The compiler merges these requests across the tree and checks that one tag name does not come from conflicting imports.

Dynamic component support remains available through reflex/…/dynamic.py. This public module re-exports the shared dynamic component behavior and forwards names that are not included in the re-export, so downstream packages can keep using the established import path while bundled-library state is managed by the active registration context.

NoSSRComponent is the contract for a component that must load on the client. Its library is kept out of static imports and loaded through a client-side wrapper. This preserves the normal Python component API while separating browser-only code from server rendering.

References: packages/…/component.py• L1885-1977 packages/…/component.py• L2292-2347 reflex/…/dynamic.py• L1-27 packages/…/dynamic.py• L18-55 reflex/…/utils.py• L79-161

Component package ecosystem

The maintained component ecosystem uses the same base contract for built-in elements, themed controls, and wrappers around third-party React libraries. Packages build on reflex-base; higher-level packages can depend on lower-level packages and expose Python-friendly names through lazy module loading.

Package layers

The package layers have different roles:

LayerSystem role
reflex-baseDefines the component contract, fields, dependency metadata, reactive values, and serialization rules.
reflex-components-coreProvides basic tree nodes such as bare values, fragments, document nodes, and shared UI building blocks.
reflex-components-internalSupplies shared behavior for maintained components, such as common class handling.
reflex-components-radixMaps themed and primitive UI controls to Radix React exports and supplies theme providers.
Other component packagesWrap focused third-party libraries such as charts, media, markdown, icons, and data grids.

Package dependency metadata expresses which layers must be installed together. Lazy mappings keep the public Python namespace simple while loading the module that owns a component only when needed.

See Component packages for package ownership, release, and dependency guidance.

References: packages/…/pyproject.toml• L1-17 packages/…/pyproject.toml• L1-14 packages/…/__init__.py• L7-19 packages/…/mappings.py• L5-53

Third-party component boundary

A third-party wrapper translates the public React API into the component contract. It chooses the React library and tag, describes accepted props and event payloads, and adds any non-standard imports or client-only loading rules. The wrapper does not replace the React library; it gives Python declarations a safe way to use it.

The custom component generator creates this boundary as a small installable package with a component source template and a demo app. Engineers who need to author a wrapper should start with Component authoring.

References: reflex/…/custom_components.py• L87-155 reflex/…/custom_components.py• L247-300 reflex/…/custom_components.py• L472-492

Use these pages to follow one concern beyond this overview:

Change checklist

When these files or sections are changed, remember to consider these:

  • A prop field change can affect Python type checks, camel-case output, event-trigger discovery, generated package types, and the wrapped React API at the same time.
  • A library, tag, alias, or default-export change must stay aligned with import conflict checks and with any package dependency or browser bundle entry.
  • An event argument change must stay aligned across the declared trigger spec, event-chain payload, and backend handler signature.
  • A component stored inside a prop or reactive value must remain visible to recursive import, hook, ref, dynamic-import, and app-wrap collection.
  • A change to client-only loading must preserve both the server-render path and the client dynamic import path.
  • A theme or provider component must keep its app-wrap priority and package-level import available to every component that relies on it.
  • A change to the public dynamic-component module must preserve re-export access for downstream packages while keeping bundled-library state scoped to the active registration context.
  • A memo output change must keep the emitted-path manifest aligned with both mirrored and unmirrored modules so removed wrappers are pruned without touching unrelated files.
  • A compile mode change must preserve the boundary between dry-run planning and real output cleanup, including stale memo files and the context module extension.

Important failure modes

TriggerConsequenceGuard
A child has an unsupported type or violates a component's nesting ruleComponent creation raises a child validation error before compilationUse supported primitive or component children and follow the wrapper's parent-child contract
A reactive value does not match the declared prop typeComponent creation raises a prop type error and no render record is producedDeclare the correct Var type or convert the expression before passing it
Two libraries provide the same tag name without a valid aliasFrontend compilation stops with an import conflictSet a distinct alias or use the correct export form
A NoSSRComponent has no libraryDynamic component compilation cannot build its client importSet the wrapped library before creating the component
A client-only dependency is treated as a static server importServer rendering can fail before the browser loads the pageUse the no-SSR component contract for browser-only libraries
A required component provider is not collected with the treeEvents, refs, or theme behavior can fail at runtime despite valid JSXKeep provider requests in the component's app-wrap metadata
A downstream package reads a dynamic-component name that the public façade does not forwardA component package can fail at import time after a base package changePreserve the façade's re-export and attribute-forwarding contract during module moves
A removed memo module remains in generated outputA stale frontend module can remain resolvable after the component is removed or movedReconcile the memo manifest and prune stale emitted paths during a real compile

Key files

FileWhy to read it
packages/…/component.pyDefines the component contract, creation rules, render record, and recursive dependency collection.
packages/…/tag.pyShows the small React-facing tag model and prop formatting boundary.
packages/…/props.pyDefines structured prop objects and their JavaScript serialization.
packages/…/dynamic.pyDefines dynamic component evaluation and the registration-scoped bundled-library behavior.
reflex/…/dynamic.pyPreserves the public dynamic-component module path for downstream packages.
reflex/…/compiler.pyConnects component metadata to page and app compilation, including generated memo output cleanup.
reflex/…/utils.pyConverts component dependencies into frontend imports and tracks generated memo files across compiles.
packages/…/templates.pyShows the output shape for component, page, and dynamic component code.
packages/…/bare.pyShows how primitive children and component values without a tag enter the tree.
packages/…/base.pyIllustrates a maintained package wrapper with aliases, theme behavior, and an app provider.
reflex/…/custom_components.pyShows the supported starting point for a third-party component package.