Skip to content

fix: using debounce inside derived#403

Open
Fruup wants to merge 3 commits intosvecosystem:mainfrom
Fruup:fix/use-debounce-inside-derived
Open

fix: using debounce inside derived#403
Fruup wants to merge 3 commits intosvecosystem:mainfrom
Fruup:fix/use-debounce-inside-derived

Conversation

@Fruup
Copy link

@Fruup Fruup commented Feb 4, 2026

Problem

The following code snippet throws an error as the internal context $state is updated when calling the debounced function. Mutating $state inside $derived is forbidden in Svelte.

const debounced = useDebounce(() => {}, 100);
const value = $derived(debounced());

Solution

For lack of a better solution, I implemented a sub-utility useDebounce.raw that does not use reactive state, so it is safe to be used inside $derived.

const debounced = useDebounce.raw(() => {}, 100);
const value = $derived(debounced());
// Does not expose debounced.pending as it is not reactive

I'm not 100% happy with this solution, so you are welcome to change it or make suggestions for improvements :)

@changeset-bot
Copy link

changeset-bot bot commented Feb 4, 2026

🦋 Changeset detected

Latest commit: f8af3aa

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
runed Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant