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

Commit 08b3e53

Browse files
docs(examples): sync InstantSearch query with React state (#3277)
1 parent f85d679 commit 08b3e53

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

examples/hooks-ssr/src/components/SearchBox.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,27 @@ export function SearchBox(props) {
1616
setValue(event.currentTarget.value);
1717
}
1818

19+
// Track when the value coming from the React state changes to synchronize
20+
// it with InstantSearch.
1921
useEffect(() => {
2022
if (query !== value) {
2123
refine(value);
2224
}
23-
// We want to track when the value coming from the React state changes
24-
// to update the InstantSearch.js query, so we don't need to track the
25-
// InstantSearch.js query.
25+
// We don't want to track when the InstantSearch query changes.
2626
// eslint-disable-next-line react-hooks/exhaustive-deps
27-
}, [refine, value]);
27+
}, [value, refine]);
28+
29+
// Track when the InstantSearch query changes to synchronize it with
30+
// the React state.
31+
useEffect(() => {
32+
// We bypass the state update if the input is focused to avoid concurrent
33+
// updates when typing.
34+
if (document.activeElement !== inputRef.current && query !== value) {
35+
setValue(query);
36+
}
37+
// We don't want to track when the React state value changes.
38+
// eslint-disable-next-line react-hooks/exhaustive-deps
39+
}, [query]);
2840

2941
return (
3042
<ControlledSearchBox

examples/hooks/components/SearchBox.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,28 @@ export function SearchBox(props: SearchBoxProps) {
1818
setValue(event.currentTarget.value);
1919
}
2020

21+
// Track when the value coming from the React state changes to synchronize
22+
// it with InstantSearch.
2123
useEffect(() => {
2224
if (query !== value) {
2325
refine(value);
2426
}
25-
// We want to track when the value coming from the React state changes
26-
// to update the InstantSearch.js query, so we don't need to track the
27-
// InstantSearch.js query.
27+
// We don't want to track when the InstantSearch query changes.
2828
// eslint-disable-next-line react-hooks/exhaustive-deps
2929
}, [value, refine]);
3030

31+
// Track when the InstantSearch query changes to synchronize it with
32+
// the React state.
33+
useEffect(() => {
34+
// We bypass the state update if the input is focused to avoid concurrent
35+
// updates when typing.
36+
if (document.activeElement !== inputRef.current && query !== value) {
37+
setValue(query);
38+
}
39+
// We don't want to track when the React state value changes.
40+
// eslint-disable-next-line react-hooks/exhaustive-deps
41+
}, [query]);
42+
3143
return (
3244
<ControlledSearchBox
3345
className={props.className}

0 commit comments

Comments
 (0)