You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -331,9 +331,13 @@ export default function Search({ indices }) {
331
331
constrootRef=createRef()
332
332
const [query, setQuery] =useState()
333
333
const [hasFocus, setFocus] =useState(false)
334
-
constsearchClient=algoliasearch(
335
-
process.env.GATSBY_ALGOLIA_APP_ID,
336
-
process.env.GATSBY_ALGOLIA_SEARCH_KEY
334
+
constsearchClient=useMemo(
335
+
() =>
336
+
algoliasearch(
337
+
process.env.GATSBY_ALGOLIA_APP_ID,
338
+
process.env.GATSBY_ALGOLIA_SEARCH_KEY
339
+
),
340
+
[]
337
341
)
338
342
339
343
useClickOutside(rootRef, () =>setFocus(false))
@@ -362,6 +366,8 @@ The `ThemeProvider` exports variables for the CSS to use (this is the [theming](
362
366
363
367
The `hasFocus` variable tracks whether the search box is currently in focus. When it is, it should display the input field (if not, only the search icon button is visible).
364
368
369
+
The `searchClient` variable is [memoized](https://reactjs.org/docs/hooks-reference.html#usememo) to avoid re-creating the Algolia search client when the `Search` component is re-rendered. This is important for performance, as the client caches searches to minimise the number of requests made to Algolia.
370
+
365
371
`StyledSearchRoot` is the root of the whole component. The React hook `useClickOutside` provides a callback if the user clicks anywhere else on the page, in which case it should close.
366
372
367
373
`InstantSearch` from [`react-instantsearch-dom`](https://community.algolia.com/react-instantsearch) wraps the search box and search results to orchestrate the search.
0 commit comments