-
-
Notifications
You must be signed in to change notification settings - Fork 118
Description
For both React and Preact, the useSignal hook implementation is currently defined as:
signals/packages/preact/src/index.ts
Lines 334 to 336 in ccb677b
| export function useSignal<T>(value: T) { | |
| return useMemo(() => signal<T>(value), []); | |
| } |
This implementation would cause the returned signal's .value to only be kept in sync with the value parameter on the very first call.
let param = 0;
// inside a compoenent
const num = useSignal(param); // num.value is 0
// re-render component with a modified param
param = 5;
const num = useSignal(param); // num.value is still 0One way to get around this behavior is to immediately set the .value on the returned signal:
signals/packages/preact/src/index.ts
Lines 70 to 71 in ccb677b
| const currentSignal = useSignal(data); | |
| currentSignal.value = data; |
I am wondering, is there a specific reason that the .value is not set directly in the useSignal function? One possible limitation I can see is that batching multiple signal updates would become not possible.
I do think however that the benefit of synchronizing between a signal's .value and its value parameter outweighs that limitation.