Skip to content

Commit 5ca36b1

Browse files
committed
ignore visited components for all_app_wrap_components (#5404)
1 parent e84c465 commit 5ca36b1

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

reflex/components/base/bare.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,29 @@ def _get_all_custom_code(self) -> set[str]:
141141
custom_code |= component._get_all_custom_code()
142142
return custom_code
143143

144-
def _get_all_app_wrap_components(self) -> dict[tuple[int, str], Component]:
144+
def _get_all_app_wrap_components(
145+
self, *, ignore_ids: set[int] | None = None
146+
) -> dict[tuple[int, str], Component]:
145147
"""Get the components that should be wrapped in the app.
146148
149+
Args:
150+
ignore_ids: The ids to ignore when collecting components.
151+
147152
Returns:
148153
The components that should be wrapped in the app.
149154
"""
150-
app_wrap_components = super()._get_all_app_wrap_components()
155+
ignore_ids = ignore_ids or set()
156+
app_wrap_components = super()._get_all_app_wrap_components(
157+
ignore_ids=ignore_ids
158+
)
151159
if isinstance(self.contents, Var):
152160
for component in _components_from_var(self.contents):
153-
if isinstance(component, Component):
154-
app_wrap_components |= component._get_all_app_wrap_components()
161+
component_id = id(component)
162+
if isinstance(component, Component) and component_id not in ignore_ids:
163+
ignore_ids.add(component_id)
164+
app_wrap_components |= component._get_all_app_wrap_components(
165+
ignore_ids=ignore_ids
166+
)
155167
return app_wrap_components
156168

157169
def _get_all_refs(self) -> set[str]:

reflex/components/component.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,24 +1976,38 @@ def _get_app_wrap_components() -> dict[tuple[int, str], Component]:
19761976
"""
19771977
return {}
19781978

1979-
def _get_all_app_wrap_components(self) -> dict[tuple[int, str], Component]:
1979+
def _get_all_app_wrap_components(
1980+
self, *, ignore_ids: set[int] | None = None
1981+
) -> dict[tuple[int, str], Component]:
19801982
"""Get the app wrap components for the component and its children.
19811983
1984+
Args:
1985+
ignore_ids: A set of component IDs to ignore. Used to avoid duplicates.
1986+
19821987
Returns:
19831988
The app wrap components.
19841989
"""
1990+
ignore_ids = ignore_ids or set()
19851991
# Store the components in a set to avoid duplicates.
19861992
components = self._get_app_wrap_components()
19871993

19881994
for component in tuple(components.values()):
1989-
components.update(component._get_all_app_wrap_components())
1995+
component_id = id(component)
1996+
if component_id in ignore_ids:
1997+
continue
1998+
ignore_ids.add(component_id)
1999+
components.update(
2000+
component._get_all_app_wrap_components(ignore_ids=ignore_ids)
2001+
)
19902002

19912003
# Add the app wrap components for the children.
19922004
for child in self.children:
2005+
child_id = id(child)
19932006
# Skip BaseComponent and StatefulComponent children.
1994-
if not isinstance(child, Component):
2007+
if not isinstance(child, Component) or child_id in ignore_ids:
19952008
continue
1996-
components.update(child._get_all_app_wrap_components())
2009+
ignore_ids.add(child_id)
2010+
components.update(child._get_all_app_wrap_components(ignore_ids=ignore_ids))
19972011

19982012
# Return the components.
19992013
return components
@@ -2206,13 +2220,23 @@ def get_component(self) -> Component:
22062220
component._add_style_recursive(style)
22072221
return component
22082222

2209-
def _get_all_app_wrap_components(self) -> dict[tuple[int, str], Component]:
2223+
def _get_all_app_wrap_components(
2224+
self, *, ignore_ids: set[int] | None = None
2225+
) -> dict[tuple[int, str], Component]:
22102226
"""Get the app wrap components for the custom component.
22112227
2228+
Args:
2229+
ignore_ids: A set of IDs to ignore to avoid infinite recursion.
2230+
22122231
Returns:
22132232
The app wrap components.
22142233
"""
2215-
return self.get_component()._get_all_app_wrap_components()
2234+
ignore_ids = ignore_ids or set()
2235+
component = self.get_component()
2236+
if id(component) in ignore_ids:
2237+
return {}
2238+
ignore_ids.add(id(component))
2239+
return self.get_component()._get_all_app_wrap_components(ignore_ids=ignore_ids)
22162240

22172241

22182242
CUSTOM_COMPONENTS: dict[str, CustomComponent] = {}

0 commit comments

Comments
 (0)