-
-
Notifications
You must be signed in to change notification settings - Fork 103
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
Stateful Components Re-used Across SPA "pages" #234
Comments
Ah, it looks like I can just use a key on my top-level route: {#key $location}
<Router {routes}/>
{/key} But my main questions remain:
|
You're correct that this is how Svelte works, by design. They try to minimize the time that components are mounted and un-mounted. It's not as much a behavior of svelte-spa-router, but more of a behavior of Svelte itself. I don't know if I would do this: {#key $location}
<Router {routes}/>
{/key} While it may work, what concerns me is that this may not scale if you need to add more keys. Right now you have one, but what about the future? If I understood your situation correctly, what I would personally do is... Just delete the state from What do you think? |
That's basically my # 1 workaround. (CommentEditor doesn't submit its own comment, but it can detect when it's being rendered on a new page by detecting when some of the variables it was passed have changed and clearing itself.) It works. It's just a non-obvious problem to run into.
This would work too. It's just not clear that it's necessary because conceptually, I'm just going to navigate away from this SPA "page" and render a new one.
I can still add I could see it maybe being a performance issue if you've got a lot of stateless components that now need to be reinitialized? But for my use case I can't see a performance difference so far. |
Thanks for confirming. Going to close this for now as something by-design and somehow app-specific. This conversation can be useful to others in the future however :) |
So, in Svelte a lot of the logic seems to be focused on top-down data passing and HTML rendering. Top-level components pass variables to inner components and inner components (re)render their contents based on the variables they're passed.
And Svelte optimizes for this use case. If I've got:
then:
This works well for the top-down data flow, but not so much when you have components with state that does not derive directly from the values they were passed. I've bumped up against this a couple times using svelte-spa-router.
So here's my scenario:
<CommentEditor />
push()
to navigate to a new page for that comment. (It's the same route as the page I'm currently on, just with different route:id
s.)<Route>
<Route>
re-uses an existing Page component and passes it newparams
.I've found a couple workarounds that work:
I can make a reactive assignment in my CommentEditor to reset its state when its properties change. (Though, then I wrote a nice bug for myself because I didn't fully understand reactive assignment precedence/ordering. And it seems error-prone to need to do this to all stateful components in your app.)
I can wrap my Page component in a big
{#key $location}
block to force all components to be re-initialized when the page changes. (But I need to remember to do this on every page-level component.)Is there some better pattern for this?
I keep thinking of these views as "pages", so I guess I expected component initialization when I navigate to a new "page". Is this something that should/could be built into svelte-spa-router?
The text was updated successfully, but these errors were encountered: