This is a pilot feature according to Algolia’s Terms of Service (“Beta Services”). Functionality may change before general availability.
<script> tag alone can’t re-initialize the experience between routes.
This guide shows how to create a custom Hook that manages the Algolia Experiences lifecycle in your React codebase.
Before you begin
You need:- A React app with a client-side router that exposes the current path on navigation, such as React Router, Remix, or TanStack Router.
- An Algolia Experience created in the dashboard. See Get started.
Declare the global type
If you use TypeScript, extend theWindow interface so window.AlgoliaExperiences is typed:
global.d.ts
Create the Hook
Create a Hook that loads the library script on first mount, callsrun() on later route changes, and calls dispose() on cleanup:
useLocation from React Router.
If your app uses a different router, swap it for the equivalent path Hook from that router.
The Hook only needs the current path as its effect dependency.
The Hook manages the experience lifecycle:
- On first mount, it builds the loader URL with your credentials as query parameters and appends the script to
<head>. - On later route changes, calls
run()to remount the experience in the current page’s container. - On unmount or before the next route change, calls
dispose()to clean up the previous instance.
Use the Hook in your layout
Call the Hook from a component that wraps all routes where you want the experience to appear.<div id="autocomplete" />) renders on the pages where the experience should appear.
Next steps
- Verify the experience renders on every route, including after browser back and forward navigation.
- Check the troubleshooting guide for content security policy directives and common issues.