-
For example, the export function useRoute(): {
path: () => string
search: () => URLSearchParams | undefined
matched: () => Route | undefined
} {
const path = $derived(router.path)
const search = $derived.by(() => {
const s = router.path.split('?')[1]
return new URLSearchParams(s)
})
const matched = $derived.by(() => {
const [path] = router.path.split('?')
const route = router.routes.find((it) => it.path === path)
if (!route) {
return
}
return route
})
return {
path: () => path,
search: () => search,
matched: () => matched,
}
} I found a strange but effective method, mainly with two problems
interface Route {
path: string
search?: URLSearchParams
matched?: RouteConfig
}
export function useRoute(): Route {
const getRoute = () => {
const [path, search] = router.path.split('?')
return {
path: router.path,
search: search ? new URLSearchParams(search) : undefined,
matched: router.routes.find((it) => it.path === path),
}
}
let r = $state(getRoute())
$effect(() => {
Object.assign(r, getRoute())
})
return r
}
const route = $derived(useRoute()) It seems that it can be written this way, but I want to know why export function useRoute(): Route {
const [path, search] = router.path.split('?')
return {
path: router.path,
search: search ? new URLSearchParams(search) : undefined,
matched: router.routes.find((it) => it.path === path),
}
}
const route = $derived(useRoute()) The best method found so far seems to be placing a reactive value in the get, so that the places where it is referenced will also remain reactive. export function useRoute(): Route {
const state = $derived.by(() => {
const [path, search] = router.path.split('?')
return {
path: router.path,
search: search ? new URLSearchParams(search) : undefined,
matched: router.routes.find((it) => it.path === path),
}
})
return {
get path() {
return state.path
},
get search() {
return state.search
},
get matched() {
return state.matched
},
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 13 replies
-
This works as expected if the router is a reactive object, e.g. export const router = $state({
path: '/path/to/somewhere?q=wah',
routes: [
{ path: '/path/to/somewhere' },
{ path: '/path/to/elsewhere' },
],
}); You can check whether the values actually update inside the function using $inspect(path, search.toString(), matched) |
Beta Was this translation helpful? Give feedback.
In Svelte, the variables themselves are reactive and if you want a reactive object to pass around, you have to wrap the variable. Probably would not do it this way, but that is one way to do it.