reflex-dev/reflex · warning · UserWarning
Do not override _add_style directly. Use add_style instead.
Error message
Do not override _add_style directly. Use add_style instead.
What it means
The style application pipeline in the builtin plugin (_apply_style) detects that a Component subclass overrode the private _add_style method instead of the public add_style hook. The private method is reserved for framework internals; overriding it breaks style composition, so Reflex raises a UserWarning as an error.
Source
Thrown at reflex/compiler/plugins/builtin.py:236
@staticmethod
def _apply_style(
comp: Component, style: ComponentStyle, page_context: PageContext
) -> Component | None:
"""Apply app-level styles to a single component.
Args:
comp: The component to style.
style: The app-level component style map.
page_context: The active page context, used to obtain a page-local
clone before rewriting ``style``.
Returns:
A page-local clone with the merged style, or ``None`` when the
component has no type-level or app-level style to apply.
"""
if type(comp)._add_style != Component._add_style:
msg = "Do not override _add_style directly. Use add_style instead."
raise UserWarning(msg)
new_style = comp._add_style()
component_style = comp._get_component_style(style)
if not new_style and not component_style:
return None
style_vars = [new_style._var_data]
if component_style:
new_style.update(component_style)
style_vars.append(component_style._var_data)
new_style.update(comp.style)
style_vars.append(comp.style._var_data)
new_style._var_data = VarData.merge(*style_vars)
owned = page_context.own(comp)
owned.style = new_style
return owned
View on GitHub (pinned to 45b8ed5ab7)
Solutions
- Rename the override from _add_style to add_style in your custom component
- Check Reflex docs/changelog for the current style-extension API for your version
- If extending a built-in component, call super().add_style() appropriately
Example fix
# before
class MyComp(rx.Component):
def _add_style(self):
return {"padding": "1em"}
# after
class MyComp(rx.Component):
def add_style(self):
return {"padding": "1em"} Defensive patterns
Strategy: type-guard
Validate before calling
assert not ("_add_style" in MyComponent.__dict__), "override add_style, not _add_style" Type guard
def uses_public_style_hook(cls) -> bool:
return "_add_style" not in cls.__dict__ Prevention
- Override add_style in custom components, never _add_style
- Consult current docs when authoring component libraries
- Run a compile smoke test for custom components in CI
When it happens
Trigger: Creating a custom Component subclass that defines _add_style() (matching it against Component._add_style via identity check). Overriding add_style() is the supported API.
Common situations: Component library authors copying internal Reflex component source that used _add_style, or code written against an older Reflex API where _add_style was the extension point.
Related errors
- Var-returning `@rx.memo` `{func_name}` cannot depend on hook
- Component-returning `@rx.memo` `{fn.__name__}` must return a
- Expected a Component, got {component_called!r} of type {type
- 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/47d5cc34b9a91457.
Report an issue: GitHub.