@@ -4,8 +4,7 @@ import { liteClient } from 'algoliasearch/lite';
44import { Hits } from 'react-instantsearch' ;
55import { InstantSearchNext } from 'react-instantsearch-nextjs' ;
66import { 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' ;
98import { SearchHit } from './SearchHit' ;
109import { SearchBox } from './SearchBox' ;
1110import { 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