Allow StatefulComponent to also be rendered via __str__ (#3211)

This makes it easier to implement React "render functions" when wrapping
components that require children to be a callable.
This commit is contained in:
Masen Furer 2024-05-01 17:03:40 -07:00 committed by GitHub
parent be93b1280c
commit aeca983290
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 1 deletions

View File

@ -174,7 +174,7 @@ def _compile_root_stylesheet(stylesheets: list[str]) -> str:
return templates.STYLE.render(stylesheets=sheets)
def _compile_component(component: Component) -> str:
def _compile_component(component: Component | StatefulComponent) -> str:
"""Compile a single component.
Args:

View File

@ -2103,6 +2103,16 @@ class StatefulComponent(BaseComponent):
"""
return dict(Tag(name=self.tag))
def __str__(self) -> str:
"""Represent the component in React.
Returns:
The code to render the component.
"""
from reflex.compiler.compiler import _compile_component
return _compile_component(self)
@classmethod
def compile_from(cls, component: BaseComponent) -> BaseComponent:
"""Walk through the component tree and memoize all stateful components.