🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
UI libraries / React InstantSearch / Widgets

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.

Signature
const serverState = await getServerState(children: ReactNode, {
  renderToString?: (node: ReactElement) => unknown,
})
Import
1
import { getServerState } from 'react-instantsearch';

About this function

If you’re looking for Next.js App Router support, a dedicated react-instantsearch-nextjs experimental package is available. Check the documentation for its usage.

getServerState() is the function that retrieves the server state to pass to <InstantSearchSSRProvider>.

Examples

1
2
3
import { getServerState } from 'react-instantsearch';

const serverState = await getServerState(<App />);

Check the server-side rendering example for full markup.

Parameters

Parameter Description
children
type: React.ReactNode

The part of the app that renders <InstantSearch>. As an optimization you can pass a minimal version of the application that only uses the hooks of the widgets used in the search, with the same options.

1
const serverState = await getServerState(<App />);
renderToString
type: (node: React.ReactElement) => unknown

The react method used to render the app. This can be renderToString from react-dom/server or an alternative method.

1
2
3
4
5
import { renderToString } from 'react-dom/server';

const serverState = await getServerState(<App />, {
  renderToString,
});

Returns
Promise

Parameter Description
serverState
type: InstantSearchServerState

The server state to pass to <InstantSearchSSRProvider>.

1
2
3
4
5
6
7
8
9
10
type InitialResult = {
  state: SearchParameters;
  results: SearchResults;
};

type InitialResults = Record<string, InitialResult>;

type InstantSearchServerState = {
  initialResults: InitialResults;
};
Did you find this page helpful?
React InstantSearch v7