Skip to content

Conversation

@Alexander-Jordan
Copy link

Update UppyContextProvider.svelte: : use $derived() instead of $state() when dealing with $props() value since it's reactive, to avoid https://svelte.dev/e/state_referenced_locally warning.

Issue: #6140

…() when dealing with $props() value since it's reactive, to avoid https://svelte.dev/e/state_referenced_locally warning.
@changeset-bot
Copy link

changeset-bot bot commented Jan 15, 2026

🦋 Changeset detected

Latest commit: 39c347b

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

This PR includes changesets to release 1 package
Name Type
@uppy/svelte Patch

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

Copy link
Member

@Murderlon Murderlon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making a PR immediately. Did you run yarn build && yarn workspace example-sveltekit dev to confirm yourself it works as expected?

@Alexander-Jordan
Copy link
Author

Thanks for making a PR immediately. Did you run yarn build && yarn workspace example-sveltekit dev to confirm yourself it works as expected?

Yes, now I did that, and I found that the warning was transferred down to line 43:

setContext(UppyContextKey, contextValue)

So I fixed it now and tested it as you described, and it works well :)

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR attempts to fix a Svelte 5 state_referenced_locally warning in UppyContextProvider.svelte by wrapping the uppy prop value in an IIFE when assigning it to the reactive $state object.

Changes:

  • Modified how the uppy prop is assigned within the $state object using an IIFE pattern
  • Added changeset documenting the fix

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
packages/@uppy/svelte/src/lib/components/headless/UppyContextProvider.svelte Modified uppy assignment in $state object to use IIFE pattern
.changeset/nice-jeans-sniff.md Added changeset documenting the fix for capturing initial value of uppy

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

// Create a single reactive context object
const contextValue: UppyContext = $state({
uppy,
uppy: (() => uppy)(),
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IIFE pattern (() => uppy)() is functionally equivalent to just uppy - it still captures only the initial value and doesn't maintain reactivity. To properly track changes to the uppy prop from $props(), you should use $derived(uppy) instead: uppy: $derived(uppy). This will ensure the contextValue.uppy updates when the prop changes.

Suggested change
uppy: (() => uppy)(),
uppy: $derived(uppy),

Copilot uses AI. Check for mistakes.
Copy link
Author

@Alexander-Jordan Alexander-Jordan Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$derived() can't be used this way, and yes this works as before but without the warning

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried Copilot is correct and the only reason you didn't get a warning anymore is because the build linter no longer matches on this code usage, which wouldn't mean this is actually fixed.

I would find it very odd if using a IIFE like this inside state is a Svelte best practice.

Copy link
Author

@Alexander-Jordan Alexander-Jordan Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I had a thought about it again, and as you say even if I get rid of the warning, technically the issue is still there...

So I tried another method: separating status and progress props so they can be declared as $state()'s, and then instead used getters/setters to get the reactive values (and I didn't give the uppy prop a setter, since it is read-only).

Honestly experimenting a lot here, I think these warnings are a bit vaguely explained in the svelte docs.

Let me know what you think about it 👍🏻

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried reproducing this error on the main branch and I can't get it to show up? Do you have a reproducible example?

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.

2 participants