Computed getter for store?
I see I have two ways computed getter for store.
1. Using 'get' inside store.
2. Creating function outside store like I did for signal.
What's better approach?
7 Replies
Whatever suits your need best. If you want to export the store, using a getter will be more elegant. If it is just inside a component, you can choose either way. That's the great thing about Solid – you have a lot of choice and there's always some helpful pattern to make your life easier.
Is adding setter inside store ok,too like this?
That would mean you would have to use store.setValue = newValue
But you can add
setValue: (v) => setMyStore('value', v)
to have a setter function, which is less confusing and works just the same.Ok, so I can use 'get', but to make setter, it's better setValue instead class's set accsessor. right?
Exactly. If you want to make everything settable, too, consider createMutable
Ryan added this to cater for people coming from Vue.
Yes, I saw createMutable. but the docs say I should use just createStore...What's better? Isn't it ani-pattern to use createMutable?
Not at all. But createStore has a few tricks up its sleeves that let you set multiple properties in one setter call.