Reactive/Store-like primitive
See original GitHub issue// Turns properties into signals
const r = reactive({ foo: 123, bar: "hehe" });
console.log(r.foo.value);
Open question: Should conversion be deep or shallow?
Pros
- Easy to make an existing object reactive
Cons
- tbd
Update with assign()-syntax
// Update a Reactive with assign()-like syntax:
const r = reactive({ name: "Alice" });
update(r, { name: "Bob" });
// property 'age' does not exist in type '{ name?: string }'
update(r, { age: 42 });
// '2' has no properties in common with '{ name?: string }'
update(r, 2);
console.log(r.name.value); // "Bob"
Pros
- Good for Redux users?
Cons
- Ambiguity for collections/arrays: Should we update or concat?
- Is this needed for an MVP or could this be added in users themselves? What’s the use case?
- Risks forking update logic
Issue Analytics
- State:
- Created a year ago
- Reactions:4
- Comments:15 (7 by maintainers)
Top Results From Across the Web
I changed my mind. Angular needs a reactive primitive
A simple reactive primitive is necessary for simple, local, reactive state synchronization.
Read more >Reactivity of primitives using reactive in Vue 3 - Stack Overflow
So just use ref as it can be used for almost everything you can even create your own types and specify them.
Read more >Guides:Reactivity - SolidJS
Solid's data management is built off a set of flexible reactive primitives which are responsible for all the updates. It takes a very...
Read more >Introducing Preact Signals: a reactive state primitive that is fast ...
It's hard but it feels like svelte is an inspiration here too. Without stores you get reactivity like… let dopeshit = 0; dopeshit...
Read more >Home Rolled Store with the Vue.js Composition API
As an alternative to reactive you could use ref but I prefer reactive for this use case. ref is typically used for primitive...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Another take at deep signals, based on Proxies.
StackBlitz with examples
I’ll refine it a bit (add tests, avoid wrapping unsupported elements and built-ins, etc) and publish an npm package so people can use it and report feedback.
I made it work as similar as possible to plain JavaScript objects, not to signals, because for my use case that’s preferable. But making it as similar as possible to signals (like @EthanStandel’s library) is also possible.
Plain JS objects
It’s using
$
to return the signal instead of the value.It writes to the signal when you update the plain object:
Signal-like objects
For primitives, it could always return the signal:
And it could force people to write to the signal using
.value
, like:I guess the main differences with @EthanStandel’s library, apart from the plain-JS-object API, are that it’s using lazy initialization (it creates proxies and signals on the fly) and it supports adding properties on the fly.
I came here to recommend this and for anyone in the mean-time, I just published a package called preact-signal-store which is meant to do just this. I expose a
deepSignal
which takes an object and makes every atomic property aSignal
and makes every parenting object aDeepSignal
type. TheDeepSignal
type haspeek
and avalue
getter and setter that act just likeSignal
but it strictly reads from and writes to the underlyingSignal
properties lower in the tree.I personally think this model would make sense in
@preact/signals
but I’m curious what maintainers and the community think