reflex-dev/reflex · error · NotImplementedError
{cls.__name__} must implement get_component to return the co
Error message
{cls.__name__} must implement get_component to return the component instance. What it means
Raised by ComponentState.get_component (via State subclass mixin) when a component-state class does not override the get_component classmethod. Reflex's ComponentState pattern requires each subclass to return the component instance it manages; the base implementation deliberately raises NotImplementedError to enforce the contract.
Source
Thrown at reflex/state.py:2565
Args:
mixin: Whether the subclass is a mixin and should not be initialized.
**kwargs: The kwargs to pass to the init_subclass method.
"""
super().__init_subclass__(mixin=mixin, **kwargs)
@classmethod
def get_component(cls, *children, **props) -> Component:
"""Get the component instance.
Args:
children: The children of the component.
props: The props of the component.
Raises:
NotImplementedError: if the subclass does not override this method.
"""
msg = f"{cls.__name__} must implement get_component to return the component instance."
raise NotImplementedError(msg)
@classmethod
def create(cls, *children, **props) -> Component:
"""Create a new instance of the Component.
Args:
children: The children of the component.
props: The props of the component.
Returns:
A new instance of the Component with an independent copy of the State.
"""
from reflex.compiler.compiler import into_component
cls._per_component_state_instance_count += 1
state_cls_name = f"{cls.__name__}_n{cls._per_component_state_instance_count}"
component_state = type(
state_cls_name,View on GitHub (pinned to 45b8ed5ab7)
Solutions
- Add a `@classmethod def get_component(cls, *children, **props)` to the subclass that returns the component instance, e.g. `return rx.text(cls.value)`
- If you don't need per-component state, inherit from rx.State instead of rx.ComponentState
- If the class is abstract intermediate, mark it abstract and only instantiate concrete leaf classes that implement get_component
Example fix
// before
class MyState(rx.ComponentState):
value: str = "hi"
// after
class MyState(rx.ComponentState):
value: str = "hi"
@classmethod
def get_component(cls, *children, **props):
return rx.text(cls.value, **props) Defensive patterns
Strategy: type-guard
Validate before calling
def has_get_component(cls) -> bool:
return callable(getattr(cls, "get_component", None)) and "get_component" not in rx.ComponentState.__dict__ or cls.get_component is not rx.ComponentState.get_component Type guard
from reflex import rx
def implements_get_component(cls: type) -> bool:
return cls.get_component is not rx.ComponentState.get_component Try / catch
try:
comp = MyComponentState.get_component()
except NotImplementedError:
# subclass missing get_component; handle or skip rendering
comp = rx.fragment() Prevention
- Treat rx.ComponentState subclasses as abstract until get_component is defined
- Add a unit test asserting each ComponentState subclass overrides get_component
When it happens
Trigger: Creating a class that inherits rx.ComponentState (or ComponentStateMixin) without defining a `get_component` classmethod, then accessing/instantiating it so the base get_component is called (typically during App.add_page or rendering of the state's component).
Common situations: Copying a rx.ComponentState example but renaming/deleting the get_component method; refactoring a component out and forgetting to keep get_component; inheriting from the mixin for utilities without intending to render a component.
Related errors
- Var-returning `@rx.memo` `{func_name}` cannot depend on hook
- Using a ComponentState as `render_fn` inside `rx.foreach` is
- ChildrenTypeError(component=cls.__name__, child=child)
- Do not override _add_style directly. Use add_style instead.
- The component `{comp_name}` cannot have `{child_name}` as a
AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28).
Data as JSON: /api/errors/5e92d160b4f96b09.
Report an issue: GitHub.