Guides / Building Search UI / UI & UX patterns

Loading indicator with React InstantSearch

This is the React InstantSearch v7 documentation. React InstantSearch v7 is the latest version of React InstantSearch and the stable version of React InstantSearch Hooks.

If you were using React InstantSearch v6, you can upgrade to v7.

If you were using React InstantSearch Hooks, you can still use the React InstantSearch v7 documentation, but you should check the upgrade guide for necessary changes.

If you want to keep using React InstantSearch v6, you can find the archived documentation.

You can display a loading indicator to inform users on a slow connection that their search is still in progress.

Use InstantSearch’s status indicator to decide when to show a loading indicator. The status can be one of the following:

  • loading: The search is in progress.
  • stalled: The search is in progress, but the response is taking longer than expected.
  • error: The search failed.
  • idle: The search succeeded.

Best practice is to display a loading indicator only when status is stalled, not during a standard (fast) search. Tweak the timing of the stalled status by setting the stalledSearchDelay option.

You can then use this condition to display a loading indicator:

1
2
3
4
5
6
7
8
function LoadingIndicator() {
  const { status } = useInstantSearch();

  if (status === 'stalled') {
    return <p>Loading search results</p>;
  }
  return null;
}

If you want the loading indicator to display as soon as the search starts, use the loading status in combination with stalled:

1
2
3
4
5
6
7
8
function LoadingIndicator() {
  const { status } = useInstantSearch();

  if (status === 'loading' || status === 'stalled') {
    return <p>Loading search results</p>;
  }
  return null;
}
Did you find this page helpful?