Skip to main content
This is the React InstantSearch v7 documentation. If you’re upgrading from v6, see the upgrade guide. If you were using React InstantSearch Hooks, this v7 documentation applies—just check for necessary changes. To continue using v6, you can find the archived documentation.
Signature
const serverState = await getServerState(children: ReactNode, {
  renderToString?: (node: ReactElement) => unknown,
})

Import

JavaScript
import { getServerState } from "react-instantsearch";

About this function

If you use Next.js’s App Router, a dedicated react-instantsearch-nextjs package is available. To learn more, see App router.
getServerState is the function that retrieves the server state to pass to <InstantSearchSSRProvider>. To learn more, see Server-side rendering.

Examples

import { getServerState } from "react-instantsearch";

const serverState = await getServerState(<App />);
Check the server-side rendering example for full markup.

Parameters

children
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.
JavaScript
const serverState = await getServerState(<App />);
renderToString
(node: React.ReactElement) => unknown
The react method used to render the app. This can be renderToString from react-dom/server or an alternative method.
JavaScript
import { renderToString } from "react-dom/server";

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

Returns

serverState
InstantSearchServerState
The server state as a Promise to pass to <InstantSearchSSRProvider>.
Type definitions
type InitialResult = {
  state: SearchParameters;
  results: SearchResults;
};

type InitialResults = Record<string, InitialResult>;

type InstantSearchServerState = {
  initialResults: InitialResults;
};
I