Switching Tabs resets a state when lazy loading is set to True
I want to keep local state of components when switching Tabs but also I need lazy=True since I don't want inactive tabs to be rendered. Unfortunately, it seems to be impossible to have both. With lazy=True the local state of the components resets to default values, in the attached example I used solara.use_state, but the same holds true for solara.use_reactive.
Is there a way to keep local state of the component when switching Tabs and having lazy loading too?
3 Replies
Hi Lisa,
I think you'd want to lift state up into the parent component, and then pass it down to the resuable component:
Does this solve your problem?
In this case i've used reactive variables, and you can also have them top level using solara.reactive
Does that mean if I have, for example, around 10 or more reactive variables across all the components in each Tab and there are 5 or more tabs, then that makes at least 50 reactive variables we have to define and pass to each tab? That would make the code complicated to work with. Is there any other way to handle this? Would it make sense to have some mechanism to keep the state within the component even when using lazy loading?
Thank you for the answer! 🙂
In your example, you were using a re-usable component. But I think you are not looking to use that, but a component that has application specific parts, is that correct? If that is the case, you could do something like this:
Here the state is outside of the component, which is usually what you want for applications (no need to make multiple instances)
This is very similar to the multipage examples in solara, does this match what you need or want?