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 / reactive-values-and-styling

Reactive values and styling

Reactive values and styling form the shared bridge between Python declarations and the browser UI. A reactive value describes a JavaScript expression that can follow application state or another expression. A style declaration turns fixed or reactive values into presentation rules for a component. Together they let components update data and appearance without rebuilding the application definition.

Reactive value model

The Var model represents a typed value that the frontend evaluates. It keeps the expression and the metadata needed to render that expression, so a value can carry state links, imports, hooks, dependencies, and required providers through the component tree. Typed value families cover scalar values as well as strings, arrays, numbers, booleans, and sets. The detailed rules for operations and dependency tracking are in Reactive variables and dependency tracking.

Reactive values can appear in component props, child content, event definitions, and styles. Literal Python values become literal Vars when the framework needs a common value form. Computed values expose a state getter as a frontend expression and can track the state fields that must cause an update. Operations on Vars build new typed expressions instead of resolving the value in Python. This lets arithmetic, comparisons, string and array transforms, and set operations remain part of the reactive expression.

Set values keep set behavior across the boundary even though the browser carries their data as an array. Set union, membership, and comparison use set semantics, while sequence behavior is available through an explicit array view. This keeps collection meaning clear when a value drives rendered content or presentation. References: packages/…/base.py• L722-883, packages/…/base.py• L1070-1089, packages/…/number.py• L61-68, packages/…/sequence.py• L66-136, packages/…/set.py• L175-343

Value propagation

Value propagation connects state reads to rendered output. The component system collects the metadata carried by Vars while it gathers props, styles, hooks, imports, and wrapper needs. This allows one expression to affect both the value shown in the browser and the frontend context required to evaluate it.

When a component tree contains reactive values, the system follows this path:

flowchart LR
    S[State field or computed value] --> V[Typed Var expression]
    V --> M[Value metadata]
    M --> C[Component prop or style]
    C --> R[Rendered frontend output]
    S --> U[State update]
    U --> R

The metadata is merged when expressions are combined. A composed value keeps the fields from every state it reads, so an update to any contributing field can refresh the result. Conflicting hook positions or wrapper roles are treated as errors because the generated frontend would not have one clear way to satisfy both requirements. References: packages/…/base.py• L331-540, packages/…/base.py• L860-883, packages/…/number.py• L506-535, packages/…/sequence.py• L659-671, packages/…/set.py• L93-112, packages/…/component.py• L1262-1278

Presentation model

Presentation is expressed as a style map attached to a component or supplied at application level. Style maps accept CSS-like keys, nested selectors, literal values, Vars, responsive values, and conditional values. The style system normalizes these inputs and emits the form used by the frontend CSS-in-JS layer. Detailed style, responsive, color, and theme behavior is in Styling and theming.

Style sources combine in a defined order. A component's built-in style comes first. Application rules for that component come next. The component's own style then takes precedence. Direct style props are part of that final component style. This relationship lets an application set shared defaults while still allowing a component instance to make a local choice.

flowchart TD
    D[Component defaults] --> M[Style merge]
    A[Application style rules] --> M
    I[Component style and direct CSS props] --> M
    M --> N[Normalized style map]
    N --> F[Frontend CSS output]

Style values can also be reactive. In that case, the style map carries the same value metadata as any other Var, so a state change can update presentation through the normal component update path. References: packages/…/component.py• L1363-1461, packages/…/style.py• L108-221, packages/…/component.py• L1463-1476

Responsive and conditional presentation

Responsive presentation assigns values to viewport ranges. A list uses the shared breakpoint order, while Breakpoints names ranges or supplies custom CSS thresholds. The style formatter converts these values to media-query rules. This keeps layout changes in the same style declaration as the base value.

Conditional presentation uses a reactive condition to choose a value or component. Color-mode conditions use the resolved light or dark mode and therefore work both in ordinary props and in style values. These mechanisms let an application adapt to viewport and user appearance state without duplicating the component tree. References: packages/…/breakpoints.py• L7-87, packages/…/style.py• L341-386, packages/…/cond.py• L221-243

Color and theme context

Theme context supplies shared appearance choices to the frontend. The application establishes a default color mode and exposes the current mode, resolved mode, and mode actions through a frontend context. Color helpers refer to theme color variables rather than hard-coded shades, so the same component can follow light and dark palettes.

The color-mode values are reactive Vars. Components can read them, switch them through event-capable actions, or select different props and children with color_mode_cond. The compile step places the default mode into the generated context and uses the top-level theme appearance when it is a literal choice. References: packages/…/style.py• L24-80, packages/…/templates.py• L361-377, packages/…/compiler.py• L182-224, packages/…/colors.py• L15-53

Component integration

Components are the boundary where reactive values and presentation meet. A component accepts Vars in supported props and keeps style as a first-class value. During rendering, it places props, event handlers, identity fields, class names, CSS output, and custom attributes on the frontend tag. The broader component contract is described in Component system.

This design gives applications one composition model for data and appearance. A state-backed value can drive text, behavior, a class name, or a CSS property. A style map can use the same expression metadata as a normal prop, so the compiler can keep the required imports and hooks with the output that uses them. References: packages/…/component.py• L776-826, packages/…/component.py• L1077-1136, packages/…/component.py• L1210-1240

Change checklist

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

  • Changes to VarData merging must be checked against both reactive component props and reactive style values. Metadata loss can produce output that looks static or lacks a required hook or import.
  • Changes to typed Var operations must be checked across literal values, state-backed values, and values embedded in collections. A type or metadata mismatch can appear only after an expression is composed.
  • Changes to set representation or array conversion must be checked against state serialization, event arguments, membership behavior, and sequence consumers. A change can silently turn set logic into ordered-array logic.
  • Changes to style precedence must be checked against component defaults, application-level component rules, and instance-level style props. A silent order change alters every component that uses shared styling.
  • Changes to breakpoint names or values must be checked against both responsive style maps and component props that consume named breakpoints.
  • Changes to color-mode context names or defaults must be checked against the style helpers, conditional rendering, generated frontend context, and theme appearance resolution.
  • Changes to dynamic class-name behavior must be checked against the chosen styling path. Tailwind needs class names present at build time, while reactive CSS values can update at run time.

Important failure modes

TriggerConsequenceGuard
A Var is used in Python if, and, or, or direct iterationThe build cannot treat the expression as a frontend valueUse reactive condition or iteration components and Var operations
A style value is an event handler or componentStyle conversion rejects the value because presentation must be a literal or VarPass a CSS value or reactive Var
Responsive values use a breakpoint name that is not definedThe style cannot map the value to a valid media queryUse the shared names or a custom CSS threshold map
A dynamic state value generates Tailwind class namesTailwind may omit the classes because it cannot see them during its build scanKeep class names static and select among declared classes
A non-literal theme appearance is used when the compile-time default is neededThe generated context falls back to the configured default color modeUse a literal theme appearance or set the application default color mode
A set is used as an array without conversionThe expression follows a deprecated array path and can lose the intended set meaningUse set operations for set logic or call to_array() for sequence behavior

Key files

FileWhy to read it
packages/…/base.pyDefines typed Vars, Var metadata, literal values, and computed values that cross the Python-to-frontend boundary.
packages/…/number.pyShows how scalar and boolean expressions preserve typed reactive behavior.
packages/…/sequence.pyShows how strings and arrays compose into reactive collection and text values.
packages/…/set.pyDefines set semantics and the explicit boundary between set and array behavior.
packages/…/dep_tracking.pyShows how state access in computed functions becomes dependency information.
packages/…/style.pyDefines style normalization, responsive formatting, pseudo selectors, and color-mode Vars.
packages/…/breakpoints.pyDefines the shared responsive breakpoint model.
packages/…/component.pyShows how components accept reactive props and merge and emit styles.
packages/…/compiler.pyResolves the application's compile-time color-mode default.
packages/…/templates.pyDefines the generated frontend context shared by state and color-mode values.
packages/…/cond.pyConnects reactive conditions to values and components, including color-mode selection.
packages/…/colors.pyDefines theme-aware color values used by style declarations.
docs/…/overview.mdGives the user-facing precedence model for global, component, and inline styling.
docs/…/responsive.mdGives the user-facing model for responsive style values and breakpoints.
docs/…/tailwind.mdDescribes the build-time constraint on dynamic Tailwind class names.