Post

I‘m really unsure if I like the linkedSignal idea. It feels so much like useEffect to synchronize two states which is kind of a code smell (cc @tkdodo.eu) but they make an interesting case…

Cédric Exbrayat@cedric-exbrayat.bsky.social

Here it is, @Angular v19 is out! 🚀 Huge release: - (most) Signal APIs are stable 🚦 - automatic migration to signals 🤯 - standalone by default - new linkedSignal and resource APIs - Huge SSR and HMR improvements And more! Check out our blog post to learn more: blog.ninja-squad.com/2024/11/19/w...

yeah I actually like the `computed` solution they are showing in the post. In React, I'd do: function ItemList({ items }) { const [item, setItem] = useState() // prefer user selection, fallback to first item const selectedItem = item ?? items[0] } deriving state better than syncing it

In their example, the selected item should reset to the first item whenever the items change. If the old item is no longer part of items, you could still do this with derivation, but if you want to always reset, when items change, then it would be tricky to get around an effect…

0 reposts1 like

Okay then I misunderstood the example, but isn't that .. bad? Like, let's I have a dropdown with 5 elements. I select the second one. Now a new element is added (background refetch), the list now has 6 elements. Why would that undo my selection?

Hm, you’re right, I can’t really think of a real world use case where you’d want that behavior… Maybe the initial gut feeling about the new linkedSignal API was right :D

We can say: fall back to the first item if the item chosen by the user is no longer valid, and that's fair, but we can still do that with derived state:

function ItemList({ items }) { const [item, setItem] = useState() const selectedItem = isValidSelection(item, items) ? item : items[0] }