Comments (6)
Interestingly the order of the props matters: repl
from svelte.
but aren't props like open or about primitives? shouldn't it run when the value changes? there is no mention of the parent object inside the $effect.
What I mean is this:
<Child open={true} {...rest} />
<button onclick={()=> rest = { open: false }}>change</button>
So if inside the effect you access open you have an implicit dependency on the object because it the object changes open could change
from svelte.
@trueadm Here's another example of a problem I think has to do with the same thing and it's weird.
Lets say we have the following component.
Test.svelte
<script lang="ts">
import { onMount } from "svelte";
let { open = $bindable(false), about, somethingElse }: { open?: boolean; about?: string; somethingElse?: number } = $props();
// $inspect(open);
$effect(() => {
console.log(open);
});
onMount(() => {
setTimeout(() => {
open = true;
}, 3000);
});
</script>
And this is our +page where I'm simulating the spreading of 2 different objects based on a condition.
<script lang="ts">
import Test from "$components/Test/Test.svelte";
import { onMount } from "svelte";
let boolVar = $state(false);
onMount(() => {
setTimeout(() => {
boolVar = true;
}, 5000);
});
</script>
<Test {...boolVar ? {} : { about: "hello", somethingElse: 2 }}></Test>
Now after 3 seconds, we are simulating a change from inside Test that sets open to true, then wait for the setTimeout inside page to change the boolVar
variable. You will notice that the variable open
which was not even in the spreaded object, retakes its default value, meaning it changes back to false.
Now if you change the $effect with an inspect then something else happens. Even though the reactivity is triggered and $inspect runs again, this time the value of open remains true.
from svelte.
Just to clarify the behaviour: the reason effects rerun when you reassign a spread is because they need to check if the new shape of the object is overriding a prop. In my PR I limited this by listening to a derived of the keys of the object so that it's only rerunning when the keys change.
However it would still need to rerun in both cases you proposed.
from svelte.
but aren't props like open or about primitives? shouldn't it run when the value changes? there is no mention of the parent object inside the $effect.
still, it shouldn't cause open
to take its default value again.
from svelte.
After discussion, we decided against merging #11290 β this is one of those cases where the cure (creating extra derived
signals indiscriminately) is worse than the disease (effects occasionally overfiring). Effects should generally be idempotent, meaning it doesn't matter if they overfire, and in the cases where it does matter you can create a derived
locally to work around the problem.
The case where a prop is reset to its default value is admittedly trickier. But #11290 doesn't actually solve it, it only mitigates it, because you could easily have a situation where the keys of the spread prop change.
If we want to fix that, I think we would need to have a new rule along the lines of 'if an unbound prop was changed locally, it won't be reset to the fallback value if the parent changes it to undefined
'.
from svelte.
Related Issues (20)
- Default values for array-like props breaks rendering HOT 1
- Svelte 5: broken reactivity in legacy mode in rare case HOT 1
- Cannot find base config file "./.svelte-kit/tsconfig.json" [tsconfig.json] HOT 2
- Svelte 5: Binding `scrollY` to `svelte:window` does not update the initial value
- Svelte 5: `contenteditable="true"` with `bind:innerText` breaks the output of `{@render children()}` HOT 5
- Allow `textContent` / `innerHTML` / `innerText` binding without setting the `contenteditable` attribute HOT 4
- Transitions break when wrapped in component. HOT 2
- A slash in a style class breaks the class directive HOT 1
- svelte-ignore regression on Svelte 5 HOT 5
- ReferenceError: Request is not defined on launch HOT 1
- Svelte 5: add examples of mixing runes and store to documention HOT 4
- In class syntax, you cannot create non-reactive variables. HOT 3
- docs: svelte 5 event handler pass variables? HOT 2
- Svelte 5: Tell if a bindable prop is actually passed using bind HOT 8
- Bad error message on `svelte:window`
- Spread props with functions wonβt work HOT 1
- Update being skipped in slot HOT 2
- VS Code: The Svelte server crashed 5 times. Cannot find module @rollup/rollup-win32-x64-msvc HOT 8
- Svelte 5: ReactiveMap and ReactiveSet has no values in console (only firefox)
- SVGs with animateTransform lag for an extremely long time when embedded HOT 7
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.