Comments (8)
You can use a class to handle external state and trigger invalidate
on it to do as you want:
class External {
#data;
#version = $state(0);
constructor(data) {
this.#data = data;
}
get data() {
this.#version;
return this.#data;
}
set data(_data) {
this.#version++;
this.#data = _data;
}
invalidate() {
this.#version++;
}
}
from svelte.
$state.frozen
doesn't seem to trigger an update either. In fact, it freezes the object, preventing the external API from mutating it.
https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAEyWOwYqDQBBEf2VocnBAsu7VjYLHfMN2DqP2ZJVJ9zDTClnx34PJsV69gtrAT4Ey1L8bsHsQ1NDFCCXoMx4hrxSUoIQsSxoOcslDmqKa4PjeIGhGaJEDqZF-No05ZXVKZ5_kn7jYdvuDjJxJr6yUVheKwjbtx5V-Ls13VVX2cE7kPQ367jdkVOnnY6yDcJZA5yD3AqFjs8TRKZk_FyMxjQgWebfIl6_PuRZKeMg4-YlGqDUttN_2F9f91XzrAAAA
from svelte.
Try to use $effect
sparsely: it should be used solely to sync a reactive variable with some imperative API. Abusing it to trigger a re fetch doesn't seem like a good idea where you could just write a function that fetch and assign and call that in the code instead of reassigning.
from svelte.
Isn't this what $state.frozen
is for?
from svelte.
You can nest the data in a simple wrapper, then create a new wrapper in the assignment.
The actual data itself will not need to be copied.
let obj = $state({ data: {} });
setInterval(() => obj = { ...obj }, 1000)
from svelte.
What are your real cases? Why do you want to do something if the data didn't change?
from svelte.
A quick and dirty fix to get the v4 behaviour (without object cloning) is to set the variable to undefined, and then reset it back to the previous state:
<script lang="ts">
let obj = $state({});
setInterval(()=> {
let tmp = obj;
obj = undefined;
obj = tmp;
}, 1000)
$effect(()=>{
obj;
console.log("An update happened")
})
</script>
from svelte.
I just had a look at the reactive collections provided by Svelte (in svelte/reactivity
). It works by using an incrementing counter similar to what trueadm suggested.
I'm glad to find some workarounds, but it would be nice to have a more idiomatic solution down the line. I'm willing to contribute a PR if the members can agree on a solution.
from svelte.
Related Issues (20)
- Svelte 5: Flaky State Ownership Warning - ownership_invalid_binding HOT 2
- Svelte 5: Assigning `this` to variable exposes signals
- svelte 5: suggestion to add an optional argument to the $effect cleanup callback to know if the effect is being destroyed or re-ran HOT 8
- Svelte 5: Whitespace problem in typed parameters to snippets HOT 2
- Svelte 5: Cannot use rest arguments in snippets HOT 1
- Using `this` alias breaks reactivity HOT 6
- Svelte 5: Chained `$derived`s don't update sometimes HOT 2
- Svelte 5: behavior of svelte-ignore is different between Svelte 4 and Svelte 5 HOT 3
- Svelte 5: faulty ownership_invalid_binding errors are back, inside #if blocks
- Svelte 5: flip animation breaks entry transition with delay HOT 1
- Svelte 5: ComponentProps does not correctly resolve conditional property types HOT 4
- A rare bind value bug sets the value to null when it is not bind in parent HOT 3
- Error de CORS en Rutas api de Sveltekit HOT 1
- Svelte 5: `<img loading="lazy">` loads eagerly in Firefox (regression) HOT 5
- Svelte 5: Two-way-binding of superforms $formData is broken if passed as arg in render snippet HOT 3
- Svelte 5: Increment(++) and Decrement(--) operator output is incorrect
- Svelte 5: differentiate between supported and unsupported TypeScript features HOT 2
- Svelte 5: Allow `$state` in object literals HOT 1
- Svelte 5: Mutating property of passed in object locks up browser HOT 13
- Svelte 5 migrate function doesn't preserve JSDoc annotation for props
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.