Creating custom signal.

I just made a simple custom signal. But is this still reactive? I know there is createMutable. but it seems it's discourage to use. So Can I use my own like this?
7 Replies
deluksic
deluksic2y ago
I don't see a reason why it wouldn't work. Out of curiosity, in which circumstance would you say this approach is better than a simple signal?
musiclover
musiclover2y ago
I just wanted to use just like createMutable...
Brendan
Brendan2y ago
Your signal wrapper should work fine. However, I believe one of the main reasons createMutable is "discouraged", is because it allows you to pass around something that can be updated anywhere it can be read - which your wrapper also allows. Personally, I like and use createMutable regularly - but only in constrained portions of our UI - definitely not for global or shared state. An example where we use it is for forms where the data is not just a flat record (eg an invoice that includes both header and detail lines), and that needs to be reactive (eg needs to react to data changes for validation or summing up detail amounts). In those cases, the data is also read and persisted as a single (atomic) graph - so the header and detail data is loaded and saved (or fails) together - and is short lived.
musiclover
musiclover2y ago
How about using only multiple signals instead of using single store for glabal state? I would like to see how you manage your huge app state..
Brendan
Brendan2y ago
This might be specific to our type of apps but we tend to see state fall into one of a few buckets: - Global state (across routes). An example might be cache of lookup data (used to select from when editing other records). This is usually provided as an "object" (via context or a global singleton) which includes read-only props to read the state and "mutation" functions to control any changes (if any are allowed) eg refetchLookup(x). Internally it can use signals or a store to hold the data - but that is really an implementation detail as it has its own specific interface for the app to use (read-only props, and "mutation functions" if any). - Route state - only exists for the duration of the route. This is a place where we might fetch a complex record and use createMutable to allow it to be edited and have reactions just within a single form. - Component state. These are usually only local to a component and signals typically suffice again here (eg toggle whether something is visible). To clarify - there can be multiple "Global states" - the "lookups" example above could be one. Another might be a "settings" singleton to provide/control persistence of application-wide settings.
ryansolid
ryansolid2y ago
Also be careful if ever incrementing in an effect sig.value++ is an infinite loop
musiclover
musiclover2y ago
Thanks Brendan for amazing lesson... It made me re-think how to structure my app.. I'm really appreciate it... Thanks Ryan. I'll remember it. You should know you're saving a lot of people's life like me.. You are god for me. Thanks... Solid is amazing..it will be web standard framework soon..