All Projects → notsidney → use-algolia

notsidney / use-algolia

Licence: MIT license
Dead-simple React hook to make Algolia search queries. Supports pagination out of the box.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to use-algolia

react-hubspot
A collection of React hooks for interacting with Hubspot APIs
Stars: ✭ 20 (-31.03%)
Mutual labels:  react-hooks, react-hook
Minimal Mistakes
📐 Jekyll theme for building a personal site, blog, project documentation, or portfolio.
Stars: ✭ 8,967 (+30820.69%)
Mutual labels:  algolia, algolia-search
useAudioPlayer
Custom React hook & context for controlling browser audio
Stars: ✭ 176 (+506.9%)
Mutual labels:  react-hooks, react-hook
react-hooks
🎣 React Hooks to get hooked on
Stars: ✭ 53 (+82.76%)
Mutual labels:  react-hooks, react-hook
usehooks-ts
React hook library, ready to use, written in Typescript.
Stars: ✭ 2,873 (+9806.9%)
Mutual labels:  react-hooks, react-hook
style-hook
use css in js with react hook.
Stars: ✭ 16 (-44.83%)
Mutual labels:  react-hooks, react-hook
gatsby-simple-blog
an easily configurable gatsby-starter-blog with overreacted looking and tags, breadcrumbs, disqus, i18n, eslint, algolia supported
Stars: ✭ 48 (+65.52%)
Mutual labels:  algolia, algolia-search
react-use-comlink
Three ways to use Comlink web workers through React Hooks (and in a typesafe manner).
Stars: ✭ 39 (+34.48%)
Mutual labels:  react-hooks, react-hook
use-async-resource
A custom React hook for simple data fetching with React Suspense
Stars: ✭ 92 (+217.24%)
Mutual labels:  react-hooks, react-hook
web
React hooks done right, for browser and SSR.
Stars: ✭ 940 (+3141.38%)
Mutual labels:  react-hooks, react-hook
Use Http
🐶 React hook for making isomorphic http requests
Stars: ✭ 2,066 (+7024.14%)
Mutual labels:  react-hooks, react-hook
use-key-hook
React hook to handle all the key press.
Stars: ✭ 27 (-6.9%)
Mutual labels:  react-hooks, react-hook
atomic-algolia
An NPM package for running atomic updates to an Algolia indices
Stars: ✭ 65 (+124.14%)
Mutual labels:  algolia, algoliasearch
react-use-scroll-position
A react hook to use scroll position
Stars: ✭ 45 (+55.17%)
Mutual labels:  react-hooks, react-hook
veact
Mutable state enhancer library for React based on @vuejs
Stars: ✭ 62 (+113.79%)
Mutual labels:  react-hooks, react-hook
algoliasearch-client-kotlin
⚡️ A fully-featured and blazing-fast Kotlin/Android API client to interact with Algolia.
Stars: ✭ 45 (+55.17%)
Mutual labels:  algolia
react-register-nodes
Register DOM Nodes in React
Stars: ✭ 32 (+10.34%)
Mutual labels:  react-hooks
react-hook-videojs
Easy React integration of Video.js using hooks.
Stars: ✭ 37 (+27.59%)
Mutual labels:  react-hooks
react-headless-tabs
Headless and highly flexible tab-like primitives built with react hooks
Stars: ✭ 107 (+268.97%)
Mutual labels:  react-hooks
react-class-hooks
React Hooks implementation for Class Components. Support React >= 15.3.2
Stars: ✭ 63 (+117.24%)
Mutual labels:  react-hooks

useAlgolia npm latest release Minified size

Dead-simple React hook to make Algolia search queries. Supports pagination out of the box.

Code snippet

Installation

Requires React >= 16.8.0 and algoliasearch 4.

yarn add react algoliasearch use-algolia

Usage

Pass the Algolia app ID, search API key, and index name to the hook call.

If you don’t have one of the three on the first hook call, pass in an empty string first. See the example below.

Optionally, pass the initial request options as the fourth argument.
See https://www.algolia.com/doc/api-reference/search-api-parameters/ for all options.

Access the returned hits, response, loading, and hasMore from searchState.

const [searchState, requestDispatch, getMore] = useAlgolia(
  APP_ID,
  SEARCH_KEY,
  INDEX_NAME,
  { query: 'construction' }
);

const { hits, response, loading, hasMore } = searchState;

Making new requests

Call requestDispatch with options to make a new Algolia search request. Note this will shallow merge your previous request options. Making new requests will immediately reset hits to an empty array and set loading to true.

requestDispatch({ query: 'bin chicken' });

// Create a new request with query: 'bin chicken' AND the filters below.
requestDispatch({ filters: 'city:Sydney OR city:Melbourne' });

// Creates a new request, resetting query and filters. Same as retrieving all objects.
requestDispatch({ query: '', filters: '' });

Loading more hits with pagination

Call getMore to get the next page of hits. Get all hits from searchState.hits, not searchState.response.hits.

You could also manually pass in page to requestDispatch to get a specific page or skip pages. Calling getMore after doing so will still work.

Increasing number of hits per page

Include hitsPerPage in your request. Initially set to Algolia’s default of 20.

const [searchState, requestDispatch, getMore] = useAlgolia(
  APP_ID,
  SEARCH_KEY,
  INDEX_NAME,
  { hitsPerPage: 100 }
);

// OR after initial query:
requestDispatch({ hitsPerPage: 100 });

Specifying hit type

If you’re using TypeScript, pass the hit type as the generic type parameter. Hits will have objectID.

type Hit = {
  title: string;
  year: number;
  actors: string[];
};

const [searchState, requestDispatch, getMore] = useAlgolia<Hit>(
  APP_ID,
  SEARCH_KEY,
  INDEX_NAME
);

// hits will have type readonly (Hit & ObjectWithObjectID)[]
const { hits } = searchState;

Changing query index or other Algolia config

When the appId, searchKey, or indexName passed to the hook call updates, the index is reinitialised with the new config.

You can also use the setAlgoliaConfig function returned by the hook:

const [, , , setAlgoliaConfig] = useAlgolia('', '', '');

setAlgoliaConfig({
  appId: APP_ID,
  searchKey: SEARCH_KEY,
  indexName: INDEX_NAME,
});

Doing either will automatically do the first query on the new index if all three items are provided.

Searching for facet values

You can access the search index created by the hook in searchState to call the searchForFacetValues method. See https://www.algolia.com/doc/api-reference/api-methods/search-for-facet-values/

Note: index may be null if one of appId, searchKey, or indexName is missing or invalid in searchState. See Changing query index or other Algolia config above.

const [searchState] = useAlgolia('', '', '');
const { index } = searchState;

if (index) index.searchForFacetValues('city');

State

Key Type Description
hits (Hit & ObjectWithObjectID)[] Contains all hits for search query, including all pages retrieved.
response SearchResponse or null Response to last query. Contains only last page of hits retrieved. Initially null. See https://www.algolia.com/doc/api-reference/api-methods/search/#response
loading boolean True if a request is being loaded, either to load initial request or when loading more hits.
hasMore boolean True if there are more pages to be retrieved.
appId string Algolia App ID.
searchKey string API key to search the index.
indexName string Algolia index to query.
index SearchIndex or null Exposed Algolia search index. Note we use the lite client, which only supports the search and searchForFacetValues methods.
request SearchOptions Exposed search request options passed to the last request. From previous requestDispatch calls.

Credits

Based on the original hook by @shamsmosowi.

Bootstrapped with tsdx and published with np.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].