preactjs / signals

Manage state with style in every framework
https://preactjs.com/blog/introducing-signals/
MIT License
3.64k stars 89 forks source link

Signal containing object need a copy to update value #436

Closed SonOfSkywalker closed 8 months ago

SonOfSkywalker commented 8 months ago

Hello,

First of all, thank you for your great work on this repo, it simplifies my code and my development work so much.

However, I encountered a problem when using signals. Unless I'm mistaken, it's not possible to update the value of an object stored in a signal without making a copy.

Example:

const data = signal({
    id: 4,
    name: "John",
    surname: "Doe",
    age: 32,
});

// won't trigger a re-render
data.value.age = 33;

// will trigger a re-render
data.value = {
    ...data.value,
    age: 33,
};

This isn't an issue for very small objects, but the performance of my application quickly degrades when I have larger objects (dict with ~100 keys): I therefore had to separate my object into several signals, but this results in rather messy code.

Is there a way to solve this problem?

Many thanks

JoviDeCroock commented 8 months ago

You could do something along the lines of deeply setting the object to be a signal, I know there are already libs like https://github.com/luisherranz/deepsignal

SonOfSkywalker commented 8 months ago

Thank you ! I will look into it

XantreDev commented 8 months ago

I've ported full vue deep reactivity tracking for preact signals: preact-signals/utils

rschristian commented 8 months ago

Going to close this out, I don't think there's anything here that we need to address.