Skip to content

Commit 66bced3

Browse files
committed
Stability hack.
1 parent fdeac4a commit 66bced3

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

components/Search/Search.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { liteClient } from 'algoliasearch/lite';
44
import { Hits } from 'react-instantsearch';
55
import { InstantSearchNext } from 'react-instantsearch-nextjs';
66
import { type SearchClient, type UiState } from 'instantsearch.js';
7-
import { useMemo } from 'react';
8-
import { useSearchParams } from 'next/navigation';
7+
import { useMemo, useEffect, useState } from 'react';
98
import { SearchHit } from './SearchHit';
109
import { SearchBox } from './SearchBox';
1110
import { SearchResultsHeader } from './SearchResultsHeader';
@@ -27,9 +26,7 @@ export const Search = ({
2726
noResultsErrorText = '',
2827
noResultsErrorTitle = '',
2928
}: SearchProps) => {
30-
const searchParams = useSearchParams();
31-
const routeKey = searchParams?.toString() || '';
32-
29+
const [ key, setKey ] = useState('search-component');
3330
const numSuggestions = useMemo(() => parseInt(suggestionsAmount, 10) || 0, [suggestionsAmount]);
3431
const algoliaClient = useMemo(() => liteClient(
3532
process.env.NEXT_PUBLIC_ALGOLIA_APP_ID,
@@ -60,12 +57,25 @@ export const Search = ({
6057
},
6158
}), [algoliaClient]);
6259

60+
console.log('Search component rendered');
61+
62+
useEffect(() => {
63+
console.log('Search component mounted');
64+
setKey(`search-component-${Date.now()}`);
65+
return () => {
66+
console.log('Search component unmounted');
67+
};
68+
}, []);
69+
6370
return (
6471
<InstantSearchNext
65-
key={routeKey}
72+
key={key}
6673
indexName={process.env.NEXT_PUBLIC_ALGOLIA_INDEX_NAME}
6774
searchClient={searchClient as SearchClient}
6875
insights
76+
future={{
77+
preserveSharedStateOnUnmount: true,
78+
}}
6979
routing={{
7080
router: {
7181
cleanUrlOnDispose: false,

0 commit comments

Comments
 (0)