reflex-dev/reflex · error · TypeError

Using a ComponentState as `render_fn` inside `rx.foreach` is

Error message

Using a ComponentState as `render_fn` inside `rx.foreach` is not supported yet.

What it means

Passing a ComponentState's `create` classmethod directly as the render function of `rx.foreach` is not supported: ComponentState creation requires special per-instance setup that the foreach render path cannot perform. It raises TypeError explicitly to fail fast.

Source

Thrown at packages/reflex-components-core/src/reflex_components_core/core/foreach.py:90

            LiteralVar.create(iterable).guess_type()
            if not isinstance(iterable, Var)
            else iterable.guess_type()
        )

        if iterable._var_type == Any:
            msg = (
                f"Could not foreach over var `{iterable!s}` of type Any. "
                "(If you are trying to foreach over a state var, add a type annotation to the var). "
                "See https://reflex.dev/docs/library/dynamic-rendering/foreach/"
            )
            raise ForeachVarError(msg)

        if (
            hasattr(render_fn, "__qualname__")
            and render_fn.__qualname__ == ComponentState.create.__qualname__
        ):
            msg = "Using a ComponentState as `render_fn` inside `rx.foreach` is not supported yet."
            raise TypeError(msg)

        if isinstance(iterable, ObjectVar):
            iterable = iterable.entries()

        if isinstance(iterable, StringVar):
            iterable = iterable.split()

        if not isinstance(iterable, ArrayVar):
            msg = (
                f"Could not foreach over var `{iterable!s}` of type {iterable._var_type}. "
                "See https://reflex.dev/docs/library/dynamic-rendering/foreach/"
            )
            raise ForeachVarError(msg)

        if types.is_optional(iterable._var_type):
            iterable = cond(iterable, iterable, [])

        component = cls._create(

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Wrap it in a lambda that calls create per item: rx.foreach(State.items, lambda item: ItemComponent.create(item))
  2. Or use a plain function/component instead of ComponentState.create inside foreach
  3. Track the Reflex GitHub issue for native ComponentState support in foreach

Example fix

# before
rx.foreach(State.items, ItemCard.create)
# after
rx.foreach(State.items, lambda item: ItemCard.create(item))
Defensive patterns

Strategy: fallback

Validate before calling

rx.foreach(State.items, (lambda it: ItemCard.create(it)) if is_component_state else render_item)

Prevention

When it happens

Trigger: `rx.foreach(State.items, ItemComponent.create)` where ItemComponent subclasses rx.ComponentState.

Common situations: Trying to render a list of ComponentState-based cards/rows; assuming create behaves like any render callable.

Related errors


AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28). Data as JSON: /api/errors/8b48b4932d92d1cd. Report an issue: GitHub.