React InstantSearch Core

7.29.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates setting up InstantSearch with a custom search box and hits display using `useInstantSearch` hooks.

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;

view raw JSON →