Extensions and custom components
Reflex provides extension points for changing compile and build behavior without changing the core app model. Engineers can add plugins that shape routes, assets, packages, and component trees. They can also publish Python packages that expose wrapped React components for reuse across apps.
This page extends the developer workflows overview. Read CLI and project workflows for command flow and deployment and hosting for runtime placement.
Extension model
Plugin lifecycle
The plugin system lets an app add behavior around the normal compile and build flow. An app registers plugin instances in rxconfig.py; Reflex then runs the active plugins as part of route collection, page compilation, frontend setup, and production output.
The main plugin capabilities are:
| Extension point | System effect |
|---|---|
| Frontend dependencies and styles | Adds packages and styles needed by the plugin. |
| Routes | Adds plugin-owned pages before page evaluation. |
| Pre-compile tasks | Creates or changes generated frontend files. |
| Page and component hooks | Reads or changes page and component compilation. |
| Environment entries | Adds values to the generated frontend environment file. |
| Static assets and post-build work | Adds files or processes the final production output. |
Built-in plugins show the intended range. Sitemap adds search-engine route data. Tailwind plugins adapt the style tool chain. EmbedPlugin changes the app from a full document app into an app mounted inside a host page. RadixThemesPlugin supplies app-level theme behavior when its components are used.
Plugin order is part of the contract. Plugins run in configuration order for most lifecycle hooks, and later plugins can override some shared outputs. Component enter hooks use explicit pre, normal, and post order. Leave hooks unwind in reverse order within each order group.
References: packages/…/base.py• L121-250 packages/…/__init__.py• L1-50 docs/…/plugins.md• L5-23 docs/…/plugins.md• L153-181
Compile pipeline
The compiler treats plugins as a chain around page and component processing. It first gives plugins a chance to evaluate a page. It then walks the page component tree and lets plugins observe or replace components before it emits frontend code.
This diagram shows the high-level plugin path from app configuration to frontend output.
flowchart LR
A[App config] --> B[Collect app pages]
B --> C[Register plugin pages]
C --> D[Evaluate pages]
D --> E[Walk component trees]
E --> F[Run page compile hooks]
F --> G[Install frontend dependencies]
G --> H[Write generated files and assets]
H --> I[Build production frontend]
I --> J[Run post-build hooks]
A plugin can return a replacement component or a component-plus-children pair when it needs to adapt the tree. The compiler keeps page-local ownership for changes, so an extension can adjust a page without changing a component instance shared by another route.
References: packages/…/compiler.py• L72-154 packages/…/compiler.py• L261-336 packages/…/compiler.py• L457-581 packages/…/compiler.py• L778-864
Route contributions
Route registration is the plugin hook for pages supplied by an extension. Reflex collects app pages first, then gives each plugin an add_page capability and a check for app-owned routes. It commits the staged plugin pages only after every route hook succeeds.
This staged process gives app code priority over an optional plugin page. A duplicate route between plugins remains an explicit conflict rather than becoming dependent on plugin order. A plugin that needs to inspect the app type can use the supplied type without receiving the mutable app object.
References: packages/…/base.py• L64-107 reflex/…/compiler.py• L1129-1160 docs/…/plugins.md• L253-296
Runtime plugin lookup
Code that needs a configured plugin instance can use rx.plugins.get_plugin(PluginType). The lookup matches subclasses and returns no value when the type is not configured. It raises a configuration error when more than one configured plugin matches, which prevents runtime behavior from silently depending on list order.
References: packages/…/base.py• L333-362 docs/…/plugins.md• L298-306
Custom component model
Component discovery
Custom components are Python-facing descriptions of frontend components. A component identifies the frontend library and component tag that Reflex must use. Its public props and event handlers let Python app code describe the component while the compiler produces the frontend use of that library.
Reflex supports component packages as separate distributions. The reflex.components namespace keeps familiar import paths working while redirecting component groups to their real packages. This lets the component ecosystem grow without forcing app code to know how the framework splits or moves component packages.
The root project declares the maintained component distributions as runtime dependencies, so installed Reflex applications can resolve the standard component groups that the namespace exposes. (pyproject.toml• L22-49)
References: packages/…/component.py• L336-358 packages/…/component.py• L776-824 reflex/…/__init__.py• L1-37 reflex/…/__init__.py• L69-111 pyproject.toml• L22-49
Component package lifecycle
The custom-component workflow turns a wrapped frontend library into a reusable Python package. reflex component init creates the package project and a demo app. The demo app provides a normal Reflex app where the author can use the component during development, and the project can install the package in editable mode for local changes.
The package normally follows this lifecycle:
- Create the project with
reflex component init. - Develop and check the component in the generated demo app.
- Use
reflex component installwhen the local package needs to be installed or re-installed in editable mode. - Build Python distributions with
reflex component build. - Publish the distributions with a package-index tool such as Twine or uv.
- Optionally share the published package and demo details with
reflex component share.
The package declares Reflex as a dependency. Its source is placed below custom_components, and the build metadata exposes the importable package inside that source tree. The build step also creates type stub files so component creation and props remain useful to Python tooling. The share step links a published package to the Reflex gallery and can include a deployed demo URL and preview image; it requires an authenticated Reflex account and package ownership.
This diagram shows how a component moves from authoring to app use.
flowchart LR
A[Wrapped React library] --> B[Python component package]
B --> C[Demo app]
B --> D[Python distribution]
D --> E[Package index]
E --> F[Reflex app installation]
F --> G[Frontend dependency and component compile]
B --> H[Reflex gallery listing]
C --> I[Deployed demo details]
I --> H
References: reflex/…/custom_components.py• L24-62 reflex/…/custom_components.py• L87-155 reflex/…/custom_components.py• L441-469 reflex/…/custom_components.py• L472-541 reflex/…/custom_components.py• L592-628 reflex/…/custom_components.py• L631-766 reflex/…/custom_components.py• L769-780 docs/…/overview.md• L25-36
Frontend library wrapping
The generated component template gives an author the core contract for a React wrapper. The wrapper names the source library, selects the exported tag, declares the props and event mappings, and can add extra frontend dependencies or code when the library needs them.
Most libraries use a normal static import. Libraries that need browser-only loading can use NoSSRComponent, which loads the library on the client instead of rendering it during server-side rendering. The export mode must match the wrapped library: named exports and default exports produce different frontend imports.
Component imports also drive frontend package setup. Reflex combines packages requested by components with packages requested by plugins, then places them in the generated frontend dependency set.
References: reflex/…/custom_components.py• L100-151 packages/…/component.py• L2205-2215 packages/…/component.py• L2292-2347 reflex/…/js_runtimes.py• L610-685
Experimental extension points
Experimental namespace
The rx._x namespace exposes features that are useful for early extension work but are not stable framework contracts. Access to the namespace emits a warning because a feature can move, change, or be removed before it is promoted.
The namespace currently exposes the legacy client-state entry point, React hook helpers, a code-block component, and hybrid_property. It also keeps temporary or deprecated aliases for promoted features such as run_in_thread and memo; those aliases warn that the stable location is now under rx. Client state is now also available through the stable rx.client_state API. The experimental module preserves the older argument order and compatibility behavior while warning that its legacy entry points are deprecated in version 0.11.0 and planned for removal in 1.0.
References: reflex/…/__init__.py• L1-78 reflex/…/client_state.py• L1-36 reflex/__init__.py• L127-156 reflex/…/memo.py• L1-11
Client-side state
client_state provides state that lives in the browser through React state rather than in a server State class. The stable API is rx.client_state(default, name=None): a named variable is global, while an unnamed variable belongs to the outermost page or @rx.memo body that uses it. A global variable shares its value and update path across component instances through a shared client reference.
The value can render in a component and can change from a frontend event. A global variable can also pass its current value to a backend event or receive a value from one. Code outside a React component can read or write global state through its JavaScript accessors. The legacy rx._x.client_state and ClientStateVar.create entry points still accept the old (var_name, default, global_ref) shape and emit a deprecation notice. The compatibility layer maps a named variable to global state, maps global_ref=False to unnamed scoped state, and generates a name when an old caller omits both the name and the false flag. This feature remains experimental in the legacy namespace, so code that depends on that compatibility path should allow for future API change.
This diagram shows the main client-state paths between a component, browser code, and backend events.
flowchart LR
A[Component state] --> B[Render value]
C[Frontend event] --> D[Set client state]
E[Backend event] --> F[Read or push global state]
G[Wrapped library or custom code] --> H[Read or write global state]
D --> A
F --> A
H --> A
References: packages/…/client_state.py• L235-349 packages/…/client_state.py• L351-536 reflex/…/client_state.py• L21-110
React hook expressions
The experimental hooks namespace provides small wrappers for common React hooks. These wrappers create frontend expressions and carry the React imports that the compiler needs.
Use this extension point when a wrapped React component needs a hook expression that the normal component props and events do not describe. It is a frontend escape hatch, not a replacement for server state or a general Python execution hook.
References: reflex/…/hooks.py• L1-91
Adaptation boundaries
Generated frontend contracts
Extensions affect more than Python imports. A plugin or component can add frontend packages, generated files, styles, environment values, routes, and code that runs after the production build. These outputs form contracts with the frontend tool chain and with any host page that consumes the result.
Plugin assets are merged into generated output, and a later output for the same path wins with a warning. Plugin environment contributions are merged after the base environment, so later plugins can override earlier values. A plugin that owns the client entry file must declare that ownership so the framework does not write a competing file.
References: packages/…/base.py• L157-250 reflex/…/build.py• L19-34 reflex/…/compiler.py• L1338-1444
Embed integration
EmbedPlugin is the clearest example of a broad framework adaptation. It changes the frontend entry behavior so the app mounts in a selected host-page element. It also emits a route manifest so the embedded app can load its page modules through a memory router.
This extension boundary matters when the app is hosted by another site. The host must provide a mount target that matches the plugin configuration, and the generated production files must remain available at the paths expected by the host.
References: packages/…/embed.py• L1-8 packages/…/embed.py• L88-114 packages/…/embed.py• L245-365
Change checklist
When these files or sections are changed, remember to consider these:
- When a plugin adds a frontend package or style, check the generated package sections and stylesheet output together. A package in the wrong section can be removed or moved on the next dependency pass.
- When a plugin owns a generated path such as the client entry file, keep its ownership signal aligned with its save task. Otherwise the framework can overwrite or race with the plugin output.
- When a plugin adds a route, check app-page precedence and plugin-to-plugin conflict behavior. Route registration is staged and is not the same as direct
App.add_pageregistration. - When a component package moves, preserve the
reflex.componentsredirect for supported import paths. A package move that only updates the new package can break existing app imports. - When a wrapper changes its frontend library, tag, export kind, or dependency list, update the Python component contract and the package metadata as one change.
- When an experimental feature is promoted, keep the old alias behavior and warning aligned with the new stable location until the compatibility path is intentionally removed.
- When the client-state API changes, keep the stable
rx.client_statecontract and the legacyrx._x.client_stateargument mapping aligned. A change to whether a variable is named or scoped can alter backend access and component sharing. - When the custom-component project template changes, keep the package metadata, editable install path, demo app import, build output, and gallery fields consistent with the package name.
- When a maintained component package is added or removed from project metadata, check the matching namespace redirect and workspace package. An installed dependency list that does not match those names can break standard component imports.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Two plugins register the same route | Route collection fails with a route conflict instead of producing a predictable owner. | Use unique plugin routes and let app-owned pages suppress optional plugin pages. |
More than one configured plugin matches get_plugin | Runtime lookup raises ConfigError. | Configure one matching instance or request a more specific plugin type. |
| A wrapped library uses the wrong named/default export setting | The generated frontend import points to the wrong export and the component cannot render. | Match is_default and the tag to the library's actual export. |
| A browser-only library uses a normal server-rendered component | The library can fail when the server evaluates code that needs browser APIs. | Wrap it with NoSSRComponent and test the client load path. |
| A plugin writes an output path already produced by another source | The later output replaces the earlier output and Reflex emits a warning. | Reserve unique paths or make the overwrite intentional and ordered. |
| A local client-state variable is used outside its component scope | The state is not available where the component expects it. Backend exchange is also rejected for a local variable. | Leave the variable unnamed for tree-local use. Give it a valid name when several component instances or backend events must share it. |
A legacy client-state caller uses global_ref=False with a named variable | The compatibility layer drops the name and creates scoped state, so code that expects backend or cross-instance access cannot address the variable. | Migrate to rx.client_state(default, name=...) and use a name only for global state. |
| A component share attempt lacks authentication or does not own the package | The gallery update is rejected and no listing details are saved. | Log in with the owning Reflex account and share the published package name. |
| A maintained component package is missing from installed project metadata | The import redirect can point to a package that is not installed in a published environment. | Keep the project dependency, namespace redirect, and workspace package name aligned. |
Key files
| File | Why to read it |
|---|---|
| packages/…/base.py | Defines the plugin contract and the contexts passed to extension hooks. |
| packages/…/compiler.py | Shows how plugins evaluate pages and walk component trees. |
| reflex/…/compiler.py | Shows where plugin routes, pre-compile work, assets, and generated frontend files enter the app lifecycle. |
| reflex/…/js_runtimes.py | Shows how component and plugin frontend dependencies become the app's installed packages. |
| packages/…/component.py | Defines the shared contract for frontend library wrappers and client-only components. |
| reflex/…/__init__.py | Shows how component imports remain discoverable across separate component packages. |
| reflex/…/custom_components.py | Defines the project, demo, install, build, and gallery-sharing workflow for published custom components. |
| packages/…/client_state.py | Defines the stable client-state model and the browser, component, and backend access paths. |
| reflex/…/__init__.py | Lists the experimental extension surface and its warning behavior. |
| reflex/…/client_state.py | Preserves the legacy client-state entry points, maps them to the stable API, and emits their deprecation notices. |
| reflex/__init__.py | Exposes stable client state and component packages through the rx namespace. |
| docs/…/plugins.md | Provides the public plugin configuration and route-hook examples. |
| docs/…/overview.md | Provides the public package lifecycle and publishing context. |
| pyproject.toml | Declares the runtime component packages that installed Reflex applications can resolve. |