Can i use signal as object? #2171
Unanswered
ShestakovViktor
asked this question in
Q&A
Replies: 1 comment
-
The only pitfall I can think of your code is that the original import { Signal, createSignal } from "solid-js";
function toObject<T>([ get, set ]: Signal<T>) {
return {
get value() { return get(); },
set value(v) { set(() => v); }
};
}
// ↓ The other overload works too!
const obj = toObject(createSignal<number>()); (I wanted both readability and not having to worry about accidentally passing a function to the setter.) WarningIf you, like me, end up liking the property approach you have to keep in mind that const [ get, set ] = createSignal(0);
set(x => x + 1); Is not the same as doing const obj = toObject(createSignal(0));
obj.value++; But rather const obj = toObject(createSignal(0));
obj.value = untrack(() => obj.value) + 1; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello. I found interesting idea to use object wrapper with signal for better code readability, but i can't assume potential pitfalls of this approach. Maybe someone more expirienced with solidjs could say something about it.
Beta Was this translation helpful? Give feedback.
All reactions