reflex-dev/reflex · error · ValueError
The component `{comp_name}` cannot have `{child_name}` as a
Error message
The component `{comp_name}` cannot have `{child_name}` as a child component What it means
BaseState.get_var_value resolves a Var's runtime value by inspecting its _var_data (state name + field name). If the var isn't attached to any state, UnretrieableVarValueError is raised because there's no state instance to read from.
Source
Thrown at packages/reflex-base/src/reflex_base/components/component.py:1623
child_name = type(child).__name__
# Iterate through the immediate children of fragment
if isinstance(child, Fragment):
for c in child.children:
validate_child(c)
if isinstance(child, Cond):
validate_child(child.children[0])
validate_child(child.children[1])
if isinstance(child, Match):
for cases in child.match_cases:
validate_child(cases[-1])
validate_child(child.default)
if self._invalid_children and child_name in self._invalid_children:
msg = f"The component `{comp_name}` cannot have `{child_name}` as a child component"
raise ValueError(msg)
if self._valid_children and child_name not in [
*self._valid_children,
*allowed_components,
]:
valid_child_list = ", ".join([
f"`{v_child}`" for v_child in self._valid_children
])
msg = f"The component `{comp_name}` only allows the components: {valid_child_list} as children. Got `{child_name}` instead."
raise ValueError(msg)
if child._valid_parents and all(
clz_name not in [*child._valid_parents, *allowed_components]
for clz_name in self._iter_parent_classes_names()
):
valid_parent_list = ", ".join([
f"`{v_parent}`" for v_parent in child._valid_parents
])View on GitHub (pinned to 45b8ed5ab7)
Solutions
- Pass a var accessed directly from a state class (`State.foo`) so _var_data carries state/field_name
- Unwrap ToOperation vars to the original state var before calling get_var_value
- Handle UnretrievableVarValueError and fetch the value from state manually
Example fix
// before val = await state.get_var_value(State.items.length()) // after val = len(state.items)
Defensive patterns
Strategy: type-guard
Validate before calling
vd = getattr(var, "_var_data", None)
if vd is None or not vd.state or not vd.field_name:
raise ValueError("var not bound to a state; cannot read value") Type guard
def var_is_state_bound(var) -> bool:
inner = var
while isinstance(inner, reflex.vars.ToOperation):
inner = inner._original
vd = inner._var_data
return vd is not None and bool(vd.state) and bool(vd.field_name) Try / catch
try:
val = await state.get_var_value(var)
except UnretrievableVarValueError:
val = None Prevention
- Pass plain State.foo vars to get_var_value
- Compute derived values server-side instead of passing var expressions
When it happens
Trigger: Calling `await state.get_var_value(some_var)` where some_var was created standalone (e.g. `rx.Var.create(...)` or a var from a non-state context) rather than accessed via a State class attribute.
Common situations: Passing a raw/computed var expression (result of arithmetic or `_var_operation`) that lost state association into server-side rendering logic like ComponentState data functions.
Related errors
- LiteralVar subclasses must implement the _var_value property
- Cannot compare a datetime object with a non-datetime object.
- Cannot access a primitive map with a Var. Consider calling r
- ChildrenTypeError(component=cls.__name__, child=child)
- Do not override _add_style directly. Use add_style instead.
AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28).
Data as JSON: /api/errors/652f82d2a8e0c1de.
Report an issue: GitHub.