You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently the useSnapshot hook returns [null, SnapshotState.WAITING, null] while waiting for the first emit, unless a default value is specified. Using null as a indicator of a missing value feels a bit weird in this case and has some technical implications as well:
We cannot assign a default value during destructuring const [value = 5] = useSnapshot(observable) as null will override the default. Because of that we need to accept defaultValue as an argument to useSnapshot.
It's hard to differentiate between no emission and null emission. undefined would suffer from the same issue, but who does emit undefined?
Additionally we use null for indication of no error and also for a missing state (whenever the source observable is nullish). I feel that if we convert one, we should probably convert all of them.
So what do we do?
replace null with undefined for value (the first element of the tuple)
replace null with undefined for state in case of a missing source observable (the middle element of the tuple)
replace null with undefined for error (the last element of the tuple)
remove const [value] = useSnapshot(observable, defaultValue) overload in favor of const [value = defaultValue] = useSnapshot(observable)
Note that any of these changes will also affect usePartialSnapshot and useAsyncMemo.
The text was updated successfully, but these errors were encountered:
I'd also be in favor of 1-3. I feel like Javascript undefined (i.e. no value assigned) is a better fit for the scenario than null (i.e. "empty" value assigned).
In terms of 4, I'm not sure I'm qualified to comment... it would depend a fair bit on where useSnapshot (and the other affected hooks) are used and how.
Currently the
useSnapshot
hook returns[null, SnapshotState.WAITING, null]
while waiting for the first emit, unless a default value is specified. Usingnull
as a indicator of a missing value feels a bit weird in this case and has some technical implications as well:const [value = 5] = useSnapshot(observable)
asnull
will override the default. Because of that we need to acceptdefaultValue
as an argument touseSnapshot
.null
emission.undefined
would suffer from the same issue, but who does emitundefined
?Additionally we use
null
for indication of no error and also for a missing state (whenever the source observable is nullish). I feel that if we convert one, we should probably convert all of them.So what do we do?
null
withundefined
forvalue
(the first element of the tuple)null
withundefined
forstate
in case of a missing source observable (the middle element of the tuple)null
withundefined
forerror
(the last element of the tuple)const [value] = useSnapshot(observable, defaultValue)
overload in favor ofconst [value = defaultValue] = useSnapshot(observable)
Note that any of these changes will also affect
usePartialSnapshot
anduseAsyncMemo
.The text was updated successfully, but these errors were encountered: