Skip to content
Discussion options

You must be logged in to vote

What you're observing is correct behavior, and $derived is actually the right tool here. Let me explain why.

Why $state doesn't reset on navigation

$state is initialized once when the component mounts. When you navigate to the same page (e.g., removing the slug from the URL), SvelteKit re-runs load() and updates the data prop — but it does not unmount and remount the component. Your $state keeps its last value because it was only initialized once, and navigation doesn't re-trigger initialization.

Why $derived works and is the correct pattern

let itemSlug = $derived(data.slug ?? null);

$derived re-computes whenever its dependencies change — including when data is updated by a new load() ca…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mig-hub
Comment options

Answer selected by mig-hub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants