Skip to content

Commit

Permalink
fix(rsc): wait for results in useSearchResults
Browse files Browse the repository at this point in the history
  • Loading branch information
aymeric-giraudet committed Dec 28, 2023
1 parent bbb27fc commit 51f45d5
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/react-instantsearch-core/src/lib/useSearchResults.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useEffect, useState } from 'react';

import { getIndexSearchResults } from './getIndexSearchResults';
import { use } from './use';
import { useIndexContext } from './useIndexContext';
import { useInstantSearchContext } from './useInstantSearchContext';
import { useRSCContext } from './useRSCContext';

import type { SearchResults } from 'algoliasearch-helper';
import type { ScopedResult } from 'instantsearch.js';
Expand All @@ -13,6 +15,7 @@ export type SearchResultsApi = {
};

export function useSearchResults(): SearchResultsApi {
const waitingForResultsRef = useRSCContext();
const search = useInstantSearchContext();
const searchIndex = useIndexContext();
const [searchResults, setSearchResults] = useState(() =>
Expand Down Expand Up @@ -41,5 +44,9 @@ export function useSearchResults(): SearchResultsApi {
};
}, [search, searchIndex]);

if (typeof window === 'undefined' && waitingForResultsRef?.current) {
use(waitingForResultsRef.current);
}

return searchResults;
}

0 comments on commit 51f45d5

Please sign in to comment.