Why is a function with $page
store reactive?
#13522
Answered
by
brunnerh
hyunbinseo
asked this question in
Q&A
-
Did not expect this to work: <!-- src/routes/+layout.svelte -->
<script lang="ts">
import { page } from '$app/stores';
const navItems = new Map([
['/a', 'A'],
['/b', 'B']
]);
const isCurrent = (href: string) => $page.url.pathname === href;
</script>
{#each navItems as [href, text]}
<a {href} style:font-weight={isCurrent(href) ? 'bold' : null}>{text}</a>
{/each} |
Beta Was this translation helpful? Give feedback.
Answered by
brunnerh
Oct 7, 2024
Replies: 1 comment 6 replies
-
This does in fact not work with Svelte < v5. It works there because reactivity is signal-based, so it can pass function and module boundaries. |
Beta Was this translation helpful? Give feedback.
6 replies
Answer selected by
hyunbinseo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does in fact not work with Svelte < v5.
It works there because reactivity is signal-based, so it can pass function and module boundaries.
The page store emits a signal and whatever calls the function can track that.