A lightweight search library with caching, pagination, and debouncing, built for React. It helps avoid unnecessary API calls while typing and provides a hook for seamless integration in React apps.
- Debounced search (avoids multiple API calls while typing)
- Built-in caching with configurable limits
- Pagination support (
loadMore) - React hook (
useSearch) for easy integration - Supports aborting in-flight requests
npm install react-searchxor
yarn add react-searchxYour API function must return an object with { items: [], total: number }.
async function myApi(query, limit, offset, signal) {
const res = await fetch(
`/api/search?q=${query}&limit=${limit}&offset=${offset}`,
{ signal }
);
return res.json(); // should be { items: [...], total: 100 }
}import React, { useState } from "react";
import useSearch from "react-searchx";
export default function App() {
const { items, loading, hasMore, setQuery, loadMore } = useSearch(myApi, {
debounceTime: 500,
limit: 10,
});
return (
<div>
<input
type="text"
placeholder="Search..."
onChange={(e) => setQuery(e.target.value)}
/>
{loading && <p>Loading...</p>}
<ul>
{items.map((item, i) => (
<li key={i}>{item.name}</li>
))}
</ul>
{hasMore && <button onClick={loadMore}>Load More</button>}
</div>
);
}import SearchLibrary from "react-searchx/lib/searchLibrary.js";
const searchLib = new SearchLibrary(myApi, { debounceTime: 400 });
searchLib.search("apple").then((res) => {
console.log(res.items); // combined items
console.log(res.total); // total count
});apiFn(query, limit, offset, signal)→ must return{ items, total }options.debounceTime→ debounce delay in ms (default400)options.limit→ number of items per page (default20)
Returns:
items: array of resultstotal: total number of resultsloading: booleanhasMore: booleansetQuery(query: string): set the search stringloadMore(): fetch the next page
Class-based API if you don’t use React.
Methods:
search(query, limit)→ fetch first page with debounceloadMore(query, limit)→ fetch next pagefetchPage(query, limit, offset)→ fetch specific page
MIT © 2025 Uditha Vithanage