Can i use signal as object? #2171
Unanswered
ShestakovViktor
asked this question in
Q&A
Replies: 2 comments
-
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
-
This is what I settled with import type { Signal } from "solid-js";
export function signalToObj<T>(signal: Signal<T>) {
return {
get: signal[0],
set: signal[1],
};
} My use case is, I'm creating signal inside a class this.signal = {
elapsedTime: signalToObj(createSignal(0)),
duration: signalToObj(createSignal(musicList[this.music].duration)),
musicInfo: signalToObj(createSignal(musicList[this.music])),
volume: signalToObj(createSignal(this.volume)),
}; |
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
Uh oh!
There was an error while loading. Please reload this page.
-
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