First approximation:
let { data } = $props();
let posts = $derived.by(() => {
let posts = $state(data.posts);
return posts;
});
...
For further actions, you may consider blocking this person and/or reporting abuse
What's the use case? What problem does this solve?
Use case is e.g. if you want a filter on your website. Like you already have post but you set a filter like posts that include a certain word -> you don't want to alter the posts variable, but the "visiblePosts" one.
Another one (my current one) is when my API delivers several "clean" data endpoints that need to be combined into a frontend. With derived I can just listen to the "clean" data and use the data that fits my frontend best.
$derivedis shallow reactiveposts = new_value,not deeply reactive
posts.title = new_value,unless you combine it with
$stateI see. But this is rarely needed, I'd say. Only if you are modifying the derived value.
Anyway, thanks for clarifying.
"rarely needed" - Actually we were using props in
$statethroughout our app, and then the new warning against doing that showed up (which did infact reveal we had issues in our code). So our first pass was to change all our$state(prop)syntax to the syntax shown in the OP. Problem with OPs approach is that if you reassign the$derviedvar, it would lose the$stateaspect. So we ended up creating aboxfunction that wraps it in a proxy with some checks and setter methods to prevent it from ever losing$state, but the whole thing is clunky. Ideally Svelte would support something natively ($derived.from(prop)or$derived.state(prop), or$state.from(prop), or something).Interesting. Indeed. Sounds like an inconsistency at the (Svelte) library level. You might want to start a discussion or an issue about it in Svelte's GH repository.
I did this small example
with just "let posts = $derived(data.posts)" it seems to work
I can't seem to reproduce the issue
see: svelte.dev/playground/f5645f6cf229...
could you please add a couple examples and explanation showing the problem you are trying to solve?
I still find confusing to understand when stuff is reactive or not
Hi
$derivedis shallow reactiveposts = new_posts,not deeply reactive
posts.title = new_title,unless you combine it with
$state