React InstantSearch Core
React InstantSearch Core is a headless UI library for React applications, enabling developers to build highly customized, lightning-fast search interfaces powered by Algolia. It provides a set of renderless components and hooks that manage the search logic and state, abstracting away the complexities of interacting directly with the Algolia API. Unlike `react-instantsearch`, which offers pre-built DOM components, `react-instantsearch-core` focuses on providing low-level primitives suitable for both web and React Native projects, allowing maximum flexibility in UI implementation. The library is currently stable at version 7.29.0 and is actively maintained within the Algolia InstantSearch ecosystem, receiving frequent updates, typically monthly, to align with new features and improvements across the InstantSearch family of libraries. Its core differentiator lies in its headless nature, empowering developers to create unique search experiences without being constrained by opinionated UI components.
Common errors
-
Cannot resolve module 'algoliasearch'
cause The `algoliasearch` package is a peer dependency but is not installed in the project.fixInstall `algoliasearch`: `npm install algoliasearch` or `yarn add algoliasearch`. -
Error: Invalid `searchClient`. It looks like `algoliasearch` was not passed to the `InstantSearch` component.
cause The `searchClient` prop of the `InstantSearch` component is either missing or an invalid Algolia search client instance.fixEnsure you are passing a valid `algoliasearch` client instance, like `algoliasearch('APP_ID', 'API_KEY')`, to the `searchClient` prop of the `InstantSearch` component. -
Error: Hooks can only be called inside of the body of a function component.
cause React Hooks like `useInstantSearch` are being called outside of a functional React component or a custom Hook.fixEnsure all calls to `useInstantSearch` (or other React Hooks) are made within the top level of a functional component or a custom Hook.
Warnings
- gotcha This package (`react-instantsearch-core`) provides renderless components and hooks for search logic. It does *not* include any visual UI components. Developers must build their own UI using the provided hooks and connectors. If you need pre-built DOM-rendering components, use `react-instantsearch` instead.
- breaking React InstantSearch v7 requires React version 16.8.0 or newer due to its reliance on React Hooks. Older React versions are not supported.
- breaking The `algoliasearch` peer dependency has specific version requirements (>= 3.1 < 6). Using an incompatible version may lead to runtime errors or unexpected behavior.
- gotcha The `InstantSearch` component requires a valid `searchClient` prop (an instance of `algoliasearch`) and an `indexName`. Incorrect or missing values will prevent search functionality.
Install
-
npm install react-instantsearch-core -
yarn add react-instantsearch-core -
pnpm add react-instantsearch-core
Imports
- InstantSearch
import InstantSearch from 'react-instantsearch-core'
import { InstantSearch } from 'react-instantsearch-core' - useInstantSearch
import { useInstantSearch } from 'react-instantsearch-core' - connectSearchBox
import { connectSearchBox } from 'react-instantsearch-core' - Hits
import { Hits } from 'react-instantsearch-core'import { Hits } from 'react-instantsearch'
Quickstart
import React, { useState, useEffect } from 'react';
import algoliasearch from 'algoliasearch';
import { InstantSearch, useInstantSearch } from 'react-instantsearch-core';
const searchClient = algoliasearch(
process.env.ALGOLIA_APP_ID ?? 'YOUR_APP_ID',
process.env.ALGOLIA_SEARCH_API_KEY ?? 'YOUR_SEARCH_API_KEY'
);
const indexName = 'your_index_name';
function CustomSearchBox() {
const { uiState, setUiState } = useInstantSearch();
const [query, setQuery] = useState(uiState.query || '');
useEffect(() => {
// Synchronize local query state with InstantSearch's UI state
if (uiState.query !== query) {
setQuery(uiState.query || '');
}
}, [uiState.query]);
const handleChange = (event) => {
const newQuery = event.target.value;
setQuery(newQuery);
setUiState(prevUiState => ({ ...prevUiState, query: newQuery }));
};
return (
<input
type="search"
placeholder="Search..."
value={query}
onChange={handleChange}
style={{ padding: '10px', width: '300px', marginBottom: '20px' }}
/>
);
}
function CustomHits() {
const { results } = useInstantSearch();
return (
<div>
{results.hits.length > 0 ? (
<ul>
{results.hits.map(hit => (
<li key={hit.objectID} style={{ marginBottom: '10px' }}>
{/* Assuming your hits have a 'name' property */}
<h3>{hit.name}</h3>
<p>{hit.description}</p>
</li>
))}
</ul>
) : (
<p>No results found.</p>
)}
</div>
);
}
function App() {
return (
<InstantSearch
searchClient={searchClient}
indexName={indexName}
initialUiState={{ [indexName]: { query: '' } }}
>
<h1>My Custom Search</h1>
<CustomSearchBox />
<CustomHits />
</InstantSearch>
);
}
export default App;