fix: only make $state variables reactive when read#15529
Conversation
🦋 Changeset detectedLatest commit: 766e6f0 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
|
Are there any realistic scenarios were this optimisation would apply, but that editor tooling wouldn't report the variable as unused? I think it's preferable to avoid adding complexity to the compiler unless there's a tangible benefit |
Well, one of the main reasons I made this was because I noticed in my bundles I would see lone |
|
Can you share a repro? Am a little surprised to hear that, but I might be misunderstanding |
The fix sounds like we need to add |
|
opened #15702 |
Currently, if a variable is wrapped in
$state, it will only actually be wrapped in$.stateif it is reassigned. However, this doesn't account for whether or not the variable is read. This PR fixes this, so that only$statevariables that are written to and read from are perceived as reactive. Here's an example:This code declares a reactive variable and assigns to it, but never reads it, meaning that it wouldn't have to be reactive. Meanwhile this...
Is reactive, since to increment count, its previous value has to be accessed.
Before submitting the PR, please make sure you do the following
feat:,fix:,chore:, ordocs:.packages/svelte/src, add a changeset (npx changeset).Tests and linting
pnpm testand lint the project withpnpm lint