Skip to content

allow memo components to be passed to props #5178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion reflex/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from rich.markup import escape

import reflex.state
from reflex import constants
from reflex.base import Base
from reflex.compiler.templates import STATEFUL_COMPONENT
from reflex.components.core.breakpoints import Breakpoints
Expand Down Expand Up @@ -1905,6 +1906,9 @@ def _register_custom_component(
Args:
component_fn: The function that creates the component.

Returns:
The custom component.

Raises:
TypeError: If the tag name cannot be determined.
"""
Expand All @@ -1928,6 +1932,7 @@ def _register_custom_component(
if dummy_component.tag is None:
raise TypeError(f"Could not determine the tag name for {component_fn!r}")
CUSTOM_COMPONENTS[dummy_component.tag] = dummy_component
return dummy_component


def custom_component(
Expand All @@ -1951,7 +1956,25 @@ def wrapper(*children, **props) -> CustomComponent:
)

# Register this component so it can be compiled.
_register_custom_component(component_fn)
dummy_component = _register_custom_component(component_fn)
object.__setattr__(
wrapper,
"_as_var",
lambda: Var(
f"jsx({dummy_component.tag})",
_var_type=Component,
_var_data=VarData(
imports={
f"$/{constants.Dirs.UTILS}/components": [
ImportVar(tag=dummy_component.tag)
],
"@emotion/react": [
ImportVar(tag="jsx"),
],
}
),
),
)

return wrapper

Expand Down
7 changes: 7 additions & 0 deletions reflex/vars/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,13 @@ def _create_literal_var(
if isinstance(value, var_subclass.python_types):
return literal_subclass.create(value, _var_data=_var_data)

if (
(as_var_method := getattr(value, "_as_var", None)) is not None
and callable(as_var_method)
and isinstance((resulting_var := as_var_method()), Var)
):
return resulting_var

from reflex.event import EventHandler
from reflex.utils.format import get_event_handler_parts

Expand Down
Loading