> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# getServerState

> React Hook for retrieving the server-side InstantSearch state in Next.js applications.

<Note>
  This is the **React InstantSearch v7** documentation.
  If you're upgrading from v6, see the [upgrade guide](/doc/guides/building-search-ui/upgrade-guides/react/#migrate-from-react-instantsearch-v6-to-react-instantsearch-v7).
  If you were using React InstantSearch Hooks,
  this v7 documentation applies—just check for [necessary changes](/doc/guides/building-search-ui/upgrade-guides/react/#migrate-from-react-instantsearch-hooks-to-react-instantsearch-v7).
  To continue using v6, you can find the [archived documentation](https://algolia.com/old-docs/deprecated/instantsearch/react/v6/api-reference/instantsearch/).
</Note>

```ts Signature theme={"system"}
const serverState = await getServerState(children: ReactNode, {
  renderToString?: (node: ReactElement) => unknown,
})
```

## Import

```jsx JavaScript icon=code theme={"system"}
import { getServerState } from "react-instantsearch";
```

## About this function

<Note>
  If you use Next.js's App Router, a dedicated
  `react-instantsearch-nextjs` package is available.
  To learn more, see [App router](/doc/guides/building-search-ui/going-further/server-side-rendering/react#with-next-js-app-router).
</Note>

`getServerState` is the function that retrieves the server state to pass to
[`<InstantSearchSSRProvider>`](/doc/api-reference/widgets/ssr-provider/react).

To learn more, see [Server-side rendering](/doc/guides/building-search-ui/going-further/server-side-rendering/react).

## Examples

<CodeGroup>
  ```js server.js theme={"system"}
  import { getServerState } from "react-instantsearch";

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

  ```jsx App.js theme={"system"}
  import { InstantSearch, InstantSearchSSRProvider } from "react-instantsearch";

  function App({ serverState }) {
    return (
      <InstantSearchSSRProvider {...serverState}>
        <InstantSearch indexName="instant_search" searchClient={searchClient}>
          {/* Widgets */}
        </InstantSearch>
      </InstantSearchSSRProvider>
    );
  }
  ```
</CodeGroup>

Check the [server-side rendering example](https://github.com/algolia/instantsearch/blob/master/examples/react/ssr/src/server.jsx) for full markup.

## Parameters

<ParamField body="children" type="React.ReactNode">
  The part of the app that renders [`<InstantSearch>`](/doc/api-reference/widgets/instantsearch/react).
  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.

  ```js JavaScript icon=code theme={"system"}
  const serverState = await getServerState(<App />);
  ```
</ParamField>

<ParamField body="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.

  ```jsx JavaScript icon=code theme={"system"}
  import { renderToString } from "react-dom/server";

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

## Returns

<ParamField body="serverState" type="InstantSearchServerState">
  The server state as a Promise to pass to [`<InstantSearchSSRProvider>`](/doc/api-reference/widgets/ssr-provider/react).

  ```ts Type definitions theme={"system"}
  type InitialResult = {
    state: SearchParameters;
    results: SearchResults;
  };

  type InitialResults = Record<string, InitialResult>;

  type InstantSearchServerState = {
    initialResults: InitialResults;
  };
  ```
</ParamField>
