Wrapping React Step by Step
This guide wraps a React color picker as a Reflex component. You will pass a Python state value to a React prop, receive a color-change event, and update the page from Python.
The example uses react-colorful, which is also used in the wrapping overview. Start with a working Reflex project from the installation guide.
1. Identify the package and export
The npm package is react-colorful, and the named React export is HexColorPicker. Reflex needs both names: library identifies the package to install, while tag identifies the component to import.
This example pins the library to [email protected]. Reflex installs this frontend dependency during compilation; you do not install it with a Python package manager.
Use is_default = True only for a default export. HexColorPicker is a named export, so the example keeps the default value of False.
2. Declare the component's props
Subclass NoSSRComponent for this browser-rendered picker. Declare color: rx.Var[str] so a literal string or a reactive Reflex value can be passed as the React color prop.
A wrapper's Python field names use snake case. Reflex translates on_change to React's onChange prop. The event declaration rx.EventHandler[lambda color: [color]] extracts the string emitted by the picker and passes it to your Python handler.
Different React components emit different arguments. Check the library's documented callback signature instead of assuming every change event contains a browser event object.
3. Connect the event to Python state
The state stores the current hex color. The set_color event handler receives the emitted string and updates that state. Passing the state back as the picker's color prop makes this a controlled component: the picker and text readout share one value.
4. Run the complete example
Move the picker below to update the color value. To use this code in an app, copy the imports, component class, state class, and wrapped_color_example function into your app module, then register the function with app.add_page(wrapped_color_example).
Selected color: #6750a4
For a standalone app, add this after the example:
app = rx.App()
app.add_page(wrapped_color_example)Run your app with reflex run, open the page, and drag the picker. The text should change along with the picker. A plain rx.Component is appropriate for wrappers that support server rendering; use NoSSRComponent when a package needs browser APIs during rendering.
Troubleshooting
Next steps
Add only the props and callbacks you need, and test them against the React library's public API. For reusable packaging, see custom components. For state and event behavior, see events and the state overview.