Blog>
Snippets

Initializing Algolia Search Client in React

Demonstrate how to initialize the Algolia Search client within a React application, setting the stage for integrating search functionalities.
import algoliasearch from 'algoliasearch/lite';
First, import the Algolia search client.
const searchClient = algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey');
Initialize the Algolia search client with your Application ID and Search-Only API Key.
import { InstantSearch } from 'react-instantsearch-dom';
Import the InstantSearch component from react-instantsearch-dom to enable the connection between your React app and Algolia.
function App() {
  return (
    <InstantSearch searchClient={searchClient} indexName="YourIndexName">
      {/* Search components will go here */}
    </InstantSearch>
  );
}
Wrap your application or the specific component that handles search with the InstantSearch component, passing it the search client and the name of the index you want to search into. This sets the stage for adding search capabilities like a search box, filters, and result displays.