reflex-dev/reflex · error · TypeError

Var-returning `@rx.memo` `{func_name}` cannot depend on hook

Error message

Var-returning `@rx.memo` `{func_name}` cannot depend on hooks. Use a component-returning `@rx.memo` instead.

What it means

ComponentState classes are marked as mixins; Reflex instantiates them only through ComponentState.create(...), which wires them into the app's state tree. Directly constructing the underlying State (e.g. `MyComponentState()`) raises ReflexRuntimeError.

Source

Thrown at packages/reflex-base/src/reflex_base/components/memo.py:808

    """Validate that a var-returning memo can compile safely.

    Args:
        return_expr: The return expression.
        func_name: The function name for error messages.

    Raises:
        TypeError: If the return expression depends on unsupported features.
    """
    var_data = VarData.merge(return_expr._get_all_var_data())
    if var_data is None:
        return

    if var_data.hooks:
        msg = (
            f"Var-returning `@rx.memo` `{func_name}` cannot depend on hooks. "
            "Use a component-returning `@rx.memo` instead."
        )
        raise TypeError(msg)

    if var_data.components:
        msg = (
            f"Var-returning `@rx.memo` `{func_name}` cannot depend on embedded "
            "components, custom code, or dynamic imports. Use a component-returning "
            "`@rx.memo` instead."
        )
        raise TypeError(msg)

    bundled_libraries = RegistrationContext.ensure_context().bundled_libraries
    for lib in dict(var_data.imports):
        if not lib:
            continue
        if lib.startswith((".", "/", "$/", "http")):
            continue
        if format.format_library_name(lib) in bundled_libraries:
            continue
        msg = (

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Use `inst = MyComponentState.create()` once, then access state via `MyComponentState.State`
  2. In tests, instantiate via create() or test through the parent app state tree
  3. Never directly construct ComponentState subclasses

Example fix

// before
state = CounterState()
// after
CounterState.create()
state = CounterState.State
Defensive patterns

Strategy: validation

Validate before calling

assert not getattr(MyComponentState, "_mixin", False) or using_create, "call ComponentState.create() instead"

Try / catch

try:
    st = MyComponentState()
except ReflexRuntimeError:
    MyComponentState.create()
    st = MyComponentState.State

Prevention

When it happens

Trigger: Calling `ComponentStateSubclass(...)` directly — including via `get_state` misuse on the mixin, pickling/reconstructing it standalone, or instantiating it in tests.

Common situations: Treating a ComponentState like a normal State in unit tests; passing the class where Reflex expects an instance created by `create()`.

Related errors


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