Skip to content

Commit c24b235

Browse files
committed
fix infinite loop
1 parent 632c00e commit c24b235

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

lib/useOnyx.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,22 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
157157
);
158158
}, [previousKey, key, options?.allowDynamicKey]);
159159

160+
// Track previous dependencies to prevent infinite loops
161+
const previousDependenciesRef = useRef<DependencyList>([]);
162+
160163
useEffect(() => {
161164
// This effect will only run if the `dependencies` array changes. If it changes it will force the hook
162165
// to trigger a `getSnapshot()` update by calling the stored `onStoreChange()` function reference, thus
163166
// re-running the hook and returning the latest value to the consumer.
167+
168+
// Deep equality check to prevent infinite loops when dependencies array reference changes
169+
// but content remains the same
170+
if (deepEqual(previousDependenciesRef.current, dependencies)) {
171+
return;
172+
}
173+
174+
previousDependenciesRef.current = dependencies;
175+
164176
if (connectionRef.current === null || isConnectingRef.current || !onStoreChangeFnRef.current) {
165177
return;
166178
}

0 commit comments

Comments
 (0)