Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork4.6k
reset state without using $effect?#16206
-
First of all, sorry if such a question has already been asked, but I have not found a similar discussion. $: tabId, (pageIndex = 0), (items = []); Now I want to write it in Svelte 5, but the only thing I was able to produce is: $effect(() => {if (tabId) {pageIndex=0;items= [];}}); However,When not to use $effect says that |
BetaWas this translation helpful?Give feedback.
All reactions
If you want deep reactivity on the items you would need to wrap the array in$state
.
This also splits logic that is coupled, so I probably would not go with this if events or function bindings cannot be used. (You could maybe bundle both variables into a single object, though that might not be the logical thing to do depending on context.)
Note that just using an$effect
for this is really is not much of an issue. For certain things, like synchronizing a local, mutable variable from a prop,$effect
has actually been recommended for some time (now that$derived
can be overwritten, you don't need that any more).
So compared to the$derived
approach, I find the$effect
code to be cleaner, an…
Replies: 1 comment 3 replies
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
You could try to use |
BetaWas this translation helpful?Give feedback.
All reactions
-
@brunnerh Thank you for your reply. The only thing I was able to come up with is as follows: const {text }: MyProps = $props();let pageIndex: number = $derived(text.length & 0);let items: MyItem[] = $derived.by(() => {voidtext;return []asMyItem[];}); Do you think it is valid / acceptable? Or is there a better way? |
BetaWas this translation helpful?Give feedback.
All reactions
-
If you want deep reactivity on the items you would need to wrap the array in Note that just using an So compared to the |
BetaWas this translation helpful?Give feedback.
All reactions
-
Thanks, the |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1