Comments (9)
Guys, an easy way to achieve that is using the following code on the svelte page that need to be rerendered:
$: if (params.id) { //watch the params.id for changes
getData(); //invoke your method to reload data
}
from svelte-spa-router.
Could it be as simple as a refresh
attribute on the route ?
const routes = {
'/': HomePage,
'/topic/:id': { component: Topic, refresh: true }
}
But that may imply that you set the component as null, render the view so Sveltes deletes the tag, then re-render with the actual component so it is initialized again. Maybe don't do that just for me, I rewrote with reactive assignments and I'm fine.
By the way, the reactive block cannot work because ps = client.asSvelteStore
is a store, so if I use $ps
later in the initialization code, I have an error because Svelte tries to initialize $ps
but at init time, ps
is undefined. But that is an edge case, I could make sure the store exists upon initialization.
from svelte-spa-router.
I made a quick repl with the issue and the workaround here: https://svelte.dev/repl/11e51e1ce65449c0b0db1af0906e206a?version=3.4.4
I think in the workaround method, you don't need to attach mount to onMount, because the reactive statement will work when params first gets its value, so it will run when the component is first mounted anyway.
The workaround (which is exactly what I figured out yesterday and works for my immediate purposes) also has the possible downside that you might want the route to run mount again even if you are just going back to the exact same route, thus not changing params. Maybe you want some random behavior, or maybe you want to update to get new information from the server. But since mount won't run unless the value of params changes, you won't see the update. (Evident in the repl example)
from svelte-spa-router.
So I think I understand why this behavior and while it wasn’t something done intentionally, it does make sense, as Svelte tries to change the DOM as little as possible.
I’ll look more into this later today or tomorrow.
In the meanwhile, maybe there’s a workaround for you?
- create a function named “mount”
- bind “mount” to onMount
- also add a reactive statement so every time that params change, you manually fire the “mount” function.
Would that work? Also, any chance you could create a REPL and share that?
from svelte-spa-router.
Thanks for writing the REPL, and yes I agree the solution you wrote is the optimal one.
Regarding this:
you might want the route to run mount again even if you are just going back to the exact same route
This is actually by design. If the navigation goes to a page with the same hash, browsers won't trigger the "hashchange" event, and so nothing happens. I actually like this behavior, as I find it makes sense for a SPA.
A workaround in your case could be to add an action to the link that changes something else in the state when clicked. You can then use that to trigger a new refresh of the view.
from svelte-spa-router.
Hello,
I have the same problem. The param in my route is not updated when I change the URL if I do this:
export let params
let { myParam } = params
So all the code depending on the URL params must be in reactive $:
statements. That results in code that is hard to understand.
A force-reload component option could be very useful !
For example, I have to do this:
$: party_id = params.party_id
$: client = partyConnect(party_id)
$: ps = client.asSvelteStore()
And then, any code that is depending on those things as well.
But in the end this is more related to a design problem in svelte than in this nice router I guess.
from svelte-spa-router.
I understand.
Reactive statements aren't necessarily "bad", however, as they're the core of Svelte. You could simplify them slightly by putting them into a reactive block like:
$: {
party_id = params.party_id
client = partyConnect(party_id)
ps = client.asSvelteStore()
}
from svelte-spa-router.
I can look into a "force reload" option however. How would that need to look like to be helpful to you?
from svelte-spa-router.
Thanks guys, this is not my first issue with svelte component remounting, and such solutions look not idiomatic really. Perhaps router should deal with this issue, not routable component.
from svelte-spa-router.
Related Issues (20)
- a11y warnings HOT 1
- Active links - allow duplicate classes
- In vite, i can't use the proxy /dev HOT 1
- Redirection from route pre-condition HOT 5
- Nested routes as maps HOT 1
- If I want to go to the login page in the sidebar + SPA form, is it possible with route guard? HOT 4
- Is this not possible in routeLoaded? HOT 5
- What is the black bar in this image? HOT 2
- Feature request: Svelte 4 support HOT 13
- FEATURE REQUEST: pass next() in precondition to decide whether the router should *change* the current route HOT 1
- Load user data HOT 4
- How to check hash-bashed history HOT 1
- Is there an alternative to the "Failed to fetch dynamically import problem" issue in Vite + Svelte spa environment? HOT 1
- WARNING: The following packages have a svelte field in their package.json but no exports condition for svelte. [email protected] HOT 8
- Typescript problematic types after "wrapping" the route HOT 2
- navigating within same component does not work HOT 5
- Typescript support: Package path ./wrap is not exported from package svelte-spa-router HOT 2
- Will the library support Svelte 5? HOT 5
- Authentication and Authorization HOT 6
- Layouts not exists for svelte-spa-router? HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from svelte-spa-router.