-
-
Notifications
You must be signed in to change notification settings - Fork 118
Description
The current implementation of useSignal uses useMemo.
signals/packages/react/src/index.ts
Lines 217 to 219 in 325499c
| export function useSignal<T>(value: T) { | |
| return useMemo(() => signal<T>(value), Empty); | |
| } |
React document says to write the code so that it still works without useMemo. Preact might be the same.
You may rely on
useMemoas a performance optimization, not as a semantic guarantee. In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works withoutuseMemo— and then add it to optimize performance.
Without useMemo, useSignal will create a new signal instance every time when re-rendering occurs and lose the previous value.
Also, useComputed uses useMemo. There's no problem I think.
signals/packages/react/src/index.ts
Lines 221 to 225 in 325499c
| export function useComputed<T>(compute: () => T) { | |
| const $compute = useRef(compute); | |
| $compute.current = compute; | |
| return useMemo(() => computed<T>(() => $compute.current()), Empty); | |
| } |