Skip to content

Commit 8d8e236

Browse files
authored
allow memo components to be passed to props (#5178)
* allow memo components to be passed to props * dang it darglint * type component
1 parent ffc9172 commit 8d8e236

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

reflex/components/component.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2199,6 +2199,9 @@ def _register_custom_component(
21992199
Args:
22002200
component_fn: The function that creates the component.
22012201
2202+
Returns:
2203+
The custom component.
2204+
22022205
Raises:
22032206
TypeError: If the tag name cannot be determined.
22042207
"""
@@ -2223,6 +2226,7 @@ def _register_custom_component(
22232226
msg = f"Could not determine the tag name for {component_fn!r}"
22242227
raise TypeError(msg)
22252228
CUSTOM_COMPONENTS[dummy_component.tag] = dummy_component
2229+
return dummy_component
22262230

22272231

22282232
def custom_component(
@@ -2246,7 +2250,24 @@ def wrapper(*children, **props) -> CustomComponent:
22462250
)
22472251

22482252
# Register this component so it can be compiled.
2249-
_register_custom_component(component_fn)
2253+
dummy_component = _register_custom_component(component_fn)
2254+
if tag := dummy_component.tag:
2255+
object.__setattr__(
2256+
wrapper,
2257+
"_as_var",
2258+
lambda: Var(
2259+
tag,
2260+
_var_type=type[Component],
2261+
_var_data=VarData(
2262+
imports={
2263+
f"$/{constants.Dirs.UTILS}/components": [ImportVar(tag=tag)],
2264+
"@emotion/react": [
2265+
ImportVar(tag="jsx"),
2266+
],
2267+
}
2268+
),
2269+
),
2270+
)
22502271

22512272
return wrapper
22522273

reflex/vars/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,13 @@ def _create_literal_var(
14751475
if isinstance(value, var_subclass.python_types):
14761476
return literal_subclass.create(value, _var_data=_var_data)
14771477

1478+
if (
1479+
(as_var_method := getattr(value, "_as_var", None)) is not None
1480+
and callable(as_var_method)
1481+
and isinstance((resulting_var := as_var_method()), Var)
1482+
):
1483+
return resulting_var
1484+
14781485
from reflex.event import EventHandler
14791486
from reflex.utils.format import get_event_handler_parts
14801487

0 commit comments

Comments
 (0)