← Lakhan Samani | Digital Garden

React useFilter Hook

There are times when we need to process and filter data in frontend. Writing the search and filter logic can be an overhead and repeating task. With the help of useFilter hook you don't need to write the filter/search logic.

Also, while dealing with large amount of data, it often takes up lot memory and keeps the main thread blocked till the filtering process is completed. This results into bad user experience. In order to keep the main thread free and run the web application without any glitches we can leverage the use of Web Worker which runs on a separate thread and can share the messages with main thread.

I, along with my friend Yash Joshi developed a react hook useFilter that lets you filter and search data using webworker.

Live Demo

How to use?


  • Yarn: yarn add @promise_learning/usefilter
  • NPM: npm install @promise_learning/usefilter


We recommend using react-window for rendering large data set. Also use useDebounce hook with search so that with every hit a search query is not triggered.

1 2 import { useFilter } from '@promise_learning/usefilter'; 3 import from './data.json'; 4 5 6 /////////////////////////////////////////// 7 // handle this using the state in your app 8 ////////////////////////////////////////// 9 10 const searchData = { 11 query: '', 12 fields: ['name'], 13 }; 14 15 const filtersData = { 16 category: ['Sci Fiction'], 17 }; 18 19 20 export const App = () => { 21 const { loading, data: result } = useFilter({ data, search: searchData, filters: filterData }); 22 23 if (loading) { 24 return <div>Loading..</div> 25 } 26 27 return ( 28 <> 29 // render result 30 </> 31 ) 32 }


  • data: Type: Array of Objects | Required: true
  • search: Type: Object -> {query: '', fields: []}. query is the search term and fields is the object keys to search on | Required: false
  • filters: Type: Object -> Key Value Pair. Where key is a field from object in array and value could be possible value | Required: false

Data Returned

Object with following data is returned by the useFilter hook.

  • loading: Type: boolean | Description: Worker processing state
  • data: Type: Array of objects | Description: filtered response based on the input

When to use?

  • Filter / Search large list in frontend
  • Filter / Search large data table in frontend

Enjoy using @promise_learning/usefilter and shower some love to github repo 🎉