How to better handle `from`'s possible `undefined` state?
I'm using
from
to observe a value from an external source. Because that's an observed value, it will be undefined at start.
Further in my code I am using the value, for instance on a <Match>
:
The problem here is that volume()
is possibly undefined, which makes TypeScript mad because comparing undefined with number is a sin.
Did I choose the wrong pattern nere? Do I need to further wrap the volume accessor in a memo to guarantee an initial value?
Shouldn't it be possible to set the initial value as such?
6 Replies
Just use
volume() ?? 0
But what if I need to actually compare the value with another number?
I ended up writing a util
You should probably apply the memo to the comparison, not the fallback, to get the most out of its reactivity filtering.
How so?
In your function, remove createMemo and put it around the comparison.
Read the part about memos: https://dev.to/lexlohr/the-zen-of-state-in-solidjs-22lj
DEV Community
The zen of state in Solid.js
Cover image from https://www.pxfuel.com/en/free-photo-jsrnn Solid.js is a frontend framework that is...