You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As a developer I would like to be able to memoize each component, such that it does not re-render in the render cycle if none of the props have changed.
There should be two ways to do this:
Use a ui.use_memo to wrap returning an element, such that the element is not re-rendered if the props have not changed. E.g. something like this should work, and not call the slow_component whenever a new colour is selected:
However, in that above example it is re-rendering the slow form each time the colour is changed. That's because the ui.use_memo wraps creating the renderable element, but the element isn't actually rendered until a render cycle (from the user) anyway.
2. Add a is_pure (or memoize?) flag to the @ui.component wrapper (defaulting to false), e.g.
React just has memo for functional components. Do we want to use something like that instead of extending it to use_memo? I don't think we'd have a good way of differentiating between use_memo for a value and use_memo for a memoized component.
@mattrunyon see my second proposal above, very similar. Or we could add ui.memo as well and then it would just be used as another decorator on top, eg.
@ui.memo@ui.componentdefmy_pure_component(...):
Come to think of it... We should almost just do the same thing with callbacks within a component... E.g.
@ui.componentdefmy_component():
@ui.callback(value) # deps as args in decoratordefmy_callback():
print("value is", value)
# vs use_callback nowdef_my_callback():
print("value is", value)
my_callback=ui.use_callback( _my_callback, [value])
...
That's not a bad idea since Python limits lambdas to a single expression. And Python already has cache decorators in functools, so it's probably not an uncommon pattern to Python devs to use decorators for caching
As a developer I would like to be able to memoize each component, such that it does not re-render in the render cycle if none of the props have changed.
There should be two ways to do this:
ui.use_memo
to wrap returning an element, such that the element is not re-rendered if the props have not changed. E.g. something like this should work, and not call the slow_component whenever a new colour is selected:However, in that above example it is re-rendering the slow form each time the colour is changed. That's because the
ui.use_memo
wraps creating the renderable element, but the element isn't actually rendered until a render cycle (from the user) anyway.2. Add a
is_pure
(ormemoize
?) flag to the@ui.component
wrapper (defaulting to false), e.g.Indicating it is a pure component should ensure that it does not re-render the component unless one of the provided props has changed.
The text was updated successfully, but these errors were encountered: