-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Closed
Description
Describe the bug
If a component receives an array, it cannot directly sort by a dynamic key to some $derived state. Changing the sort key is non-reactive.
e.g. changing the value of order with changeOrder doesn't work unless we spread the items. This isn't obvious as the result of .sort(...) is a new value - so I'm not sure why the value it acts on would need to be new (and not directly using a prop).
let { items } = $props();
let order = $state(true);
// Doesn't work
// let sortedItems = $derived(items.sort((a, b) => order ? (a - b) : (b -a)))
// Works
let sortedItems = $derived([...items].sort((a, b) => order ? a.localeCompare(b) : b.localeCompare(a)))
function changeOrder() {
order = !order;
}Reproduction
Logs
No response
System Info
ChromeSeverity
annoyance
Metadata
Metadata
Assignees
Labels
No labels