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
- Use `inst = MyComponentState.create()` once, then access state via `MyComponentState.State`
- In tests, instantiate via create() or test through the parent app state tree
- 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
- Always obtain ComponentState instances via create()
- Access state through the .State attribute in tests
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
- reflex.Config.plugins entry {entry.__name__!r} could not be
- Using a ComponentState as `render_fn` inside `rx.foreach` is
- Do not override _add_style directly. Use add_style instead.
- Only one of `data` or `fp` may be provided, not both.
- At least one of `data` or `fp` must be provided.
AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28).
Data as JSON: /api/errors/9bde956d5f823ad8.
Report an issue: GitHub.