Repository navigation
How to wrap the “class” into createSignal/Store #1474
|
I am a newer of solid, and I want to create a list of reactive items which every item is a object. I can't wrap a instance whatever it is a custom class or library provides (as *1), Although I can embed this by the Spread Syntax(as *2), but maybe there is a better way. And if i used the Spread Syntax, i can't get the instance with type info any more. Appreciate any help even if point out errors in thinking or code. thanks very much. import { Component, For} from 'solid-js';
import { createStore } from 'solid-js/store';
let Demo:Component =()=>{
let [items, setItems] = createStore<any[]>([]); // a list of object to show
setItems([ // init items value
new Map, // (*1) Class can not wrapped
{...new Map()}] // (*2) but can wrapped through spread
);
let changeItem = (index:number)=>{ // click to make instance value change
setItems(index, "v", Math.random())
}
return (
<For each={items}>
{(item, index)=>(
<div onClick={[changeItem,index()]}>
click_to_change_value : {item.v}
</div>
)}
</For>
)
}
export default Demo; |
Replies: 2 comments 11 replies
|
There's |
|
Stores can be classes, they just won't wrap child classes. But once they are marked as proxy they will work as a child of other stores and will wrap. Solid exports a symbol that when present on an object(or class instance) will pre-empt the stores not wrapping behavior. What I'm saying is you can always: const [store, setStore] = createStore(new MyClass());Now that isn't too ergonomic but you could wrap that in a function: function storify(someClassInstance) {
const [store] = createStore(someClassInstance);
return store;
}And then: setItems(i => [...i, storify(new MyClass())])Now in the case of certain types of objects I wouldn't expect much. Just because something is a store doesn't mean its methods are bound to a reactive version of |
Stores can be classes, they just won't wrap child classes. But once they are marked as proxy they will work as a child of other stores and will wrap. Solid exports a symbol that when present on an object(or class instance) will pre-empt the stores not wrapping behavior.
What I'm saying is you can always:
Now that isn't too ergonomic but you could wrap that in a function:
And then:
Now in the case of certain types of objects I wouldn't expect much. Just because something is a store doesn…