Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
fix(hooks): avoid effect in useStableValue (#3670)
Browse files Browse the repository at this point in the history
  • Loading branch information
Haroenv authored Nov 2, 2022
1 parent 7366327 commit d1f53ae
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions packages/react-instantsearch-hooks/src/lib/useStableValue.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { useEffect, useState } from 'react';
import { useState } from 'react';

import { dequal } from '../lib/dequal';

export function useStableValue<TValue>(value: TValue) {
const [stableValue, setStableValue] = useState<TValue>(() => value);

useEffect(() => {
if (!dequal(stableValue, value)) {
setStableValue(value);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value]);
if (!dequal(stableValue, value)) {
setStableValue(value);
}

return stableValue;
}

0 comments on commit d1f53ae

Please sign in to comment.