Skip to content

Commit 2aff668

Browse files
authored
Merge pull request Expensify#701 from Expensify/Rory-RemoveUseLiveRef
Remove useLiveRef
2 parents 8c27f11 + 6cc86e3 commit 2aff668

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

lib/useLiveRef.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ 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.
69
*/
710
function useLiveRef<T>(value: T) {
811
const ref = useRef<T>(value);

lib/useOnyx.ts

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

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

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

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

9294
return (input: OnyxValue<TKey> | undefined): TReturnValue => {
9395
const currentDependencies = currentDependenciesRef.current;
94-
const currentSelector = currentSelectorRef.current;
9596

9697
// Recompute if input changed, dependencies changed, or first time
9798
const dependenciesChanged = !shallowEqual(lastDependencies, currentDependencies);
9899
if (!hasComputed || lastInput !== input || dependenciesChanged) {
99100
// Only proceed if we have a valid selector
100-
if (currentSelector) {
101-
const newOutput = currentSelector(input);
101+
if (selector) {
102+
const newOutput = selector(input);
102103

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

113114
return lastOutput;
114115
};
115-
}, [currentDependenciesRef, currentSelectorRef, options?.selector]);
116+
}, [currentDependenciesRef, selector]);
116117

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

tests/unit/useOnyxTest.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,6 @@ 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)
413412
rerender(undefined);
414413

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

0 commit comments

Comments
 (0)