Comments (9)
writing to an object's prop doesn't count as reading the object
If that were the case, clicking the second button would cause {object.count}
inside the first to become undefined
(because the effect wouldn't re-run after assigning to object
), which would obviously be a bug:
<script>
let object = $state({});
let count = $state(0);
$effect(() => {
console.log('in effect');
object.count = count;
});
</script>
<button onclick={() => count++}>
clicks: {object.count}
</button>
<button onclick={() => object = {}}>
reset object
</button>
from svelte.
That's absurd — the resulting behaviour would make no sense at all! And what if you wrote it like this, for whatever reason? Does object
suddenly become a dependency of the effect, because it's no longer immediately assigned to?
$effect(() => {
const obj = object;
obj.count = count;
});
Or does some magical static analysis happen that somehow determines that object
isn't actually "used", it's only written to? In which case what happens here?
function update_object() {
const obj = object;
obj.count = count;
}
$effect(() => {
update_object();
});
There's only one sensible behaviour here, and it's the one that's currently implemented. The solution here, if you really need to resort to using an effect (i.e. you can't bind directly to condition.playerId
or whatever) is to use untrack
.
from svelte.
Maybe I should use <select bind:value={condition.playerId}>
, right?
from svelte.
playerId
gets updated to "1"
probably because there is no undefined
among the options. Then, in effect, you modify condition
, and it seems $effect subs to condition
as well, causing an infinite loop.
Though it's incorrect usage of $effect, it looks like a bug that $effect subs to condition
despite on only writing to it.
from svelte.
This is expected behaviour — condition
is state, and you're reading and writing to it in the same effect
from svelte.
(use untrack
if you want to, well, untrack the read)
from svelte.
Wasn't there a fix that writing to an object's prop doesn't count as reading the object?
Edit:
let obj = $state({});
$effect.pre(() => {
obj.prop = {}
});
$inspect(obj);
no loop, but
let obj = $state({});
$effect.pre(() => {
obj = {}
obj.prop = {}
});
$inspect(obj);
causes loop
from svelte.
I neither think that is obvious, nor that it necessarily should work that way.
To me the effect reads as having one dependency: count
from svelte.
Just goes to show that effects are not particularly intuitive.
You also don't need untrack
here since the whole object is assigned.
$effect.pre(() => {
- condition = { };
+ const newCondition = { };
if (playerId) {
- condition.playerId = playerId;
+ newCondition.playerId = playerId;
}
+
+ condition = newCondition;
});
from svelte.
Related Issues (20)
- Production build failure of [email protected] -> [email protected] HOT 5
- Svelte 5: next 182 breaks Threlte apps HOT 2
- Svelte 5: Regression since 5.0.0-next.171 HOT 4
- Rerendering bug in table with sticky first column HOT 2
- Svelte 5: Usage of `$effect.active` in `.svelte.js` files gives incorrect error in runtime HOT 2
- Svelte 5: Rune to Store and vice versa HOT 3
- Svelte 5: decide on behaviour of `$state.frozen` HOT 20
- Reactivity regression since 5.0.0-next.177 HOT 6
- Error on mounting to detached element with `css: injected` HOT 2
- Animation is not applied inside nested global css selector HOT 5
- svelte 5 : structural clone failed HOT 2
- `list` is not reactive, but `list.length` is HOT 11
- Svelte 5: Portal'd content and multiple elements in same conditional HOT 3
- `SvelteDate.now()` as a live updating source HOT 6
- `<svelte:component>` wrong error message on component creation HOT 2
- Children duplication of list items rendered within components after v `5.0.0-next.179`
- Svelte 5: cannot change nested objects if not binded HOT 26
- [Svelte 5] `state_unsafe_mutation` when using class getters that perform side effects HOT 3
- Svelte 5: using inlang Paraglide JS message in <svelte:head> meta description tag causes error in SSR
- Rest parameters in a snippet should be a compile error HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from svelte.