reflex-dev/reflex · error · ValueError
Match condition {condition_index} of case {case_index} canno
Error message
Match condition {condition_index} of case {case_index} cannot be a component. What it means
In rx.match, conditions (all tuple elements except the last) must be vars or values convertible to vars, not components. Components are only allowed as the return value (last element) of a case.
Source
Thrown at packages/reflex-components-core/src/reflex_components_core/core/match.py:167
The processed match cases.
Raises:
ValueError: If the default case is not the last case or the tuple elements are less than 2.
"""
match_cases: list[tuple[list[Var], BaseComponent | Var]] = []
for case_index, case in enumerate(cases):
# There should be at least two elements in a case tuple(a condition and return value)
if len(case) < 2:
msg = "A case tuple should have at least a match case element and a return value."
raise ValueError(msg)
*conditions, return_value = case
conditions_vars: list[Var] = []
for condition_index, condition in enumerate(conditions):
if isinstance(condition, BaseComponent):
msg = f"Match condition {condition_index} of case {case_index} cannot be a component."
raise ValueError(msg)
conditions_vars.append(cls._create_case_var_with_var_data(condition))
return_value = (
cls._create_case_var_with_var_data(return_value)
if not isinstance(return_value, BaseComponent)
else return_value
)
if not isinstance(return_value, (Var, BaseComponent)):
msg = "Return value must be a var or component"
raise ValueError(msg)
match_cases.append((conditions_vars, return_value))
return match_cases
@classmethod
def _validate_return_types(View on GitHub (pinned to 45b8ed5ab7)
Solutions
- Reorder so the component is the last element: (condition, component_return_value)
- If the condition must render something, compute it as a var (e.g. a computed var or expression) instead of a component
Example fix
# before
rx.match(State.n, (rx.text("one"), 1))
# after
rx.match(State.n, (1, rx.text("one"))) Defensive patterns
Strategy: validation
Validate before calling
from reflex.components.base.bare import Bare from reflex.components import Component conditions = case[:-1] assert not any(isinstance(c, Component) for c in conditions), 'conditions cannot be components'
Type guard
def has_component_condition(case: tuple) -> bool:
return any(isinstance(c, Component) for c in case[:-1]) Prevention
- Remember only the LAST tuple element is the return value
- Components belong at the end of the case tuple
When it happens
Trigger: Passing a component as a non-final tuple element, e.g. rx.match(cond, (rx.text("a"), rx.text("b"))) where the first rx.text is treated as a condition rather than a return value.
Common situations: Miscounting tuple positions: intending (return_value, condition) order, or forgetting that only the last element is the return value when a case has one condition.
Related errors
- For cases with return types as Vars, a default case must be
- The condition must be set
- rx.match should have at least one case.
- rx.match should have tuples of cases and one default case as
- A case tuple should have at least a match case element and a
AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28).
Data as JSON: /api/errors/bca3fee060511400.
Report an issue: GitHub.