Skip to content
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

fix: do not trigger re-render due to unneeded state in context #386

Merged
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
6 changes: 3 additions & 3 deletions solara/components/applayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ def _set_sidebar_default(updater: Callable[[PortalElements], PortalElements]):

class ElementPortal:
def __init__(self):
self.context = solara.create_context((cast(PortalElements, {}), _set_sidebar_default))
self.context = solara.create_context(_set_sidebar_default)

# TODO: can we generalize the use of 'portals' ? (i.e. transporting elements from one place to another)
def use_portal(self) -> List[Element]:
portal_elements, set_portal_elements = solara.use_state(cast(PortalElements, {}))
self.context.provide((portal_elements, set_portal_elements)) # type: ignore
self.context.provide(set_portal_elements) # type: ignore

portal_elements_flat: List[Tuple[int, Element]] = []
for uuid, value in portal_elements.items():
Expand All @@ -47,7 +47,7 @@ def use_portal(self) -> List[Element]:

def use_portal_add(self, children: List[Element], offset: int):
key = solara.use_unique_key(prefix="portal-")
portal_elements, set_portal_elements = solara.use_context(self.context)
set_portal_elements = solara.use_context(self.context)
values: List[Tuple[int, Element]] = []
for i, child in enumerate(children):
values.append((offset + i, child))
Expand Down
6 changes: 3 additions & 3 deletions solara/components/title.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def _set_titles_default(updater: Callable[[Titles], Titles]):
pass


titles_context = solara.create_context((cast(Titles, {}), _set_titles_default))
titles_context = solara.create_context(_set_titles_default)


def use_title_get() -> Optional[str]:
titles, set_titles = solara.use_state(cast(Titles, {}))
titles_context.provide((titles, set_titles)) # type: ignore
titles_context.provide(set_titles) # type: ignore
if titles:
title = max([(order, title) for (key, (order, title)) in titles.items()], key=lambda x: x[0])[1]
else:
Expand All @@ -36,7 +36,7 @@ def use_title_get() -> Optional[str]:

def use_title_set(title: str, offset: int):
key = solara.use_unique_key(prefix="title-")
_titles, set_titles = solara.use_context(titles_context)
set_titles = solara.use_context(titles_context)

def update():
set_titles(lambda titles: {**titles, key: (offset, title)})
Expand Down
Loading