Skip to content

Commit a5a47ea

Browse files
Revert "Remove useLiveRef"
1 parent 6f35534 commit a5a47ea

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed

lib/useLiveRef.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ import {useRef} from 'react';
33
/**
44
* Creates a mutable reference to a value, useful when you need to
55
* maintain a reference to a value that may change over time without triggering re-renders.
6-
*
7-
* @deprecated This hook breaks the Rules of React, and should not be used.
8-
* The migration effort to remove it safely is not currently planned.
96
*/
107
function useLiveRef<T>(value: T) {
118
const ref = useRef<T>(value);

lib/useOnyx.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,11 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
7878
const previousKey = usePrevious(key);
7979

8080
const currentDependenciesRef = useLiveRef(dependencies);
81-
const selector = options?.selector;
81+
const currentSelectorRef = useLiveRef(options?.selector);
8282

8383
// Create memoized version of selector for performance
8484
const memoizedSelector = useMemo(() => {
85-
if (!selector) {
86-
return null;
87-
}
85+
if (!options?.selector) return null;
8886

8987
let lastInput: OnyxValue<TKey> | undefined;
9088
let lastOutput: TReturnValue;
@@ -93,13 +91,14 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
9391

9492
return (input: OnyxValue<TKey> | undefined): TReturnValue => {
9593
const currentDependencies = currentDependenciesRef.current;
94+
const currentSelector = currentSelectorRef.current;
9695

9796
// Recompute if input changed, dependencies changed, or first time
9897
const dependenciesChanged = !shallowEqual(lastDependencies, currentDependencies);
9998
if (!hasComputed || lastInput !== input || dependenciesChanged) {
10099
// Only proceed if we have a valid selector
101-
if (selector) {
102-
const newOutput = selector(input);
100+
if (currentSelector) {
101+
const newOutput = currentSelector(input);
103102

104103
// Deep equality mode: only update if output actually changed
105104
if (!hasComputed || !deepEqual(lastOutput, newOutput) || dependenciesChanged) {
@@ -113,7 +112,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
113112

114113
return lastOutput;
115114
};
116-
}, [currentDependenciesRef, selector]);
115+
}, [currentDependenciesRef, currentSelectorRef, options?.selector]);
117116

118117
// Stores the previous cached value as it's necessary to compare with the new value in `getSnapshot()`.
119118
// We initialize it to `null` to simulate that we don't have any value from cache yet.

tests/unit/useOnyxTest.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ describe('useOnyx', () => {
409409
expect(result.current[1].status).toEqual('loaded');
410410

411411
selector = (entry: OnyxEntry<{id: string; name: string}>) => `id - ${entry?.id}, name - ${entry?.name} - selector changed`;
412+
// In a react app we expect the selector ref to change during a rerender (see selectorRef/useLiveRef)
412413
rerender(undefined);
413414

414415
expect(result.current[0]).toEqual('id - test_id, name - test_name - selector changed');

0 commit comments

Comments
 (0)