reflex-dev/reflex · error · ValueError
The condition must be set.
Error message
The condition must be set.
What it means
`rx.cond(condition, c1, c2)` needs a condition that can be converted into a Var. `LiteralVar.create(condition)` returning None means the condition is None or of a type Reflex cannot render as a literal — so there is nothing to branch on.
Source
Thrown at packages/reflex-components-core/src/reflex_components_core/core/cond.py:190
def cond(condition: Any, c1: Any, c2: Any = types.Unset(), /) -> Component | Var:
"""Create a conditional component or Prop.
Args:
condition: The cond to determine which component to render.
c1: The component or prop to render if the cond_var is true.
c2: The component or prop to render if the cond_var is false.
Returns:
The conditional component.
Raises:
ValueError: If the arguments are invalid.
"""
# Convert the condition to a Var.
cond_var = LiteralVar.create(condition)
if cond_var is None:
msg = "The condition must be set."
raise ValueError(msg)
# If the first component is a component, create a Cond component.
if isinstance(c1, BaseComponent):
if not isinstance(c2, types.Unset) and not isinstance(c2, BaseComponent):
return Cond.create(cond_var.bool(), c1, Fragment.create(c2))
return Cond.create(cond_var.bool(), c1, c2)
# Otherwise, create a conditional Var.
# Check that the second argument is valid.
if isinstance(c2, BaseComponent):
return Cond.create(cond_var.bool(), Fragment.create(c1), c2)
if isinstance(c2, types.Unset):
msg = "For conditional vars, the second argument must be set."
raise ValueError(msg)
# convert the truth and false cond parts into vars so the _var_data can be obtained.
c1_var = Var.create(c1)
c2_var = Var.create(c2)View on GitHub (pinned to 45b8ed5ab7)
Solutions
- Pass a real boolean expression or State Var: rx.cond(State.is_logged_in, ...)
- Default None conditions: rx.cond(condition if condition is not None else False, ...)
- Ensure the condition is a supported type (bool, int, str, Var, comparison result)
Example fix
// before rx.cond(State.user, greeting, login_form) # user is None or unsupported type // after rx.cond(State.user is not None, greeting, login_form)
Defensive patterns
Strategy: type-guard
Validate before calling
if condition is None:
condition = False
comp = rx.cond(condition, c1, c2) Type guard
def is_condable(c) -> bool:
return c is not None and (isinstance(c, (bool, int, float, str, Var)) or hasattr(c, '_var_type') or callable(getattr(c, '__eq__', None))) Prevention
- Initialize state fields used as conditions with real defaults
- Prefer explicit comparisons (State.x is not None) over raw object truthiness
When it happens
Trigger: `rx.cond(None, a, b)`, `rx.cond(untyped_object, ...)`, or passing a value whose type LiteralVar.create does not handle.
Common situations: Conditionally building UI where the condition variable was never initialized; passing a Python object (dict/dataclass) instead of a bool, comparison, or Var; refactoring leaves the condition expression empty.
Related errors
- Var-returning `@rx.memo` `{func_name}` cannot depend on embe
- Invalid prop(s) {invalid_fields_str} for {component_name!r}.
- default_color_mode must be one of {allowed_color_modes}, but
- Invalid event chain: {value!s} of type {value._var_type}
- Invalid event: {v}
AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28).
Data as JSON: /api/errors/d334a263cfebf09e.
Report an issue: GitHub.