preactjs / signals

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

singal .changed() #353

Closed MarcWeber closed 11 months ago

MarcWeber commented 1 year ago
const count = signal([]);
count.value.append(2)
count.changed()

Imagine the data type being more complex than an array. Then how to tell that a value changed ? Yes you could put the value into an array and change that and reassign, but ...

So why not just have a changed() function signaling even if its same object something changed ?

rschristian commented 1 year ago

So why not just have a changed() function signaling even if its same object something changed ?

Because simplicity of the API is quite important.

I don't see a .changed() in any other popular reactivity system, so I'm not convinced it's a compelling addition here.

Thiagolino8 commented 1 year ago

This is close to what Svelte does, instead of tracking object reference changes svelte tracks assignments So you can mutate an object, array, map, set and simply assign it to itself so the reactivity works without having to create new objects

let grades = new map()
grades.set('math', 5)
grades = grades //this is reactive in svelte
XantreDev commented 1 year ago

I thinks we should provide custom equality checkers support, to allow create more complex wrappers for signals (Map, Set, array)

monaye commented 1 year ago

how everyone updating the complex object with nested component setup without re-rendering all components? memo seems to be not behaving correctly if I use the signal.

  const complexTodos = useSignal([ 
     { id: 1, title: 'my first task', complete : { completed: true, markedBy: user1}, history: [ { date: xxx, updatedBy: user1 } ] }},
     { id: 2, title: 'my first task', complete : { completed: false, markedBy: user1}, history: [ { date: xxx, updatedBy: user1 } ] } }},
     { id: 3, title: 'my first task', complete : { completed: true, markedBy: user2}, history: [ { date: xxx, updatedBy: user2 } ] } }}
  ]);
rschristian commented 1 year ago

how everyone updating the complex object with nested component setup without re-rendering all components? memo seems to be not behaving correctly if I use the signal.

@monaye This isn't relevant to the current issue, please open a new one.

When you do, please provide examples. "memo seems to not be behaving correctly" is not enough to go on.

andrewiggins commented 11 months ago

If you'd prefer to reactive mutable objects, check out the deepsignal package to create reactive objects and arrays. I'm not sure if it supports Map or Set. For those you'd probably need to do something similar to deep signal and proxy mutation methods on those objects to trigger subscriptions.

We don't have plans at the moment to support these patterns in the core API, so closing for now.