# react-use-search

> React hook for searching and filtering data

Latest version **0.3.2** (published 2021-12-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-use-search
pnpm add react-use-search
yarn add react-use-search
bun add react-use-search
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2021-12-08 |
| First published | 2019-03-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=8 |
| Dependencies | 2 |
| Unpacked size | 347.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 18 |
| Author | emilbaekdahl |
| Maintainers | emilbaekdahl, frederikspang |
| Keywords | react, search, filter, hook, typescript |

## Links

- npm: https://www.npmjs.com/package/react-use-search
- Repository: https://github.com/prograsdk/react-use-search
- npm.io page: https://npm.io/package/react-use-search

## Dependencies (2)

- [lodash.isequal](https://npm.io/package/lodash.isequal.md) ^4.5.0
- [lodash.debounce](https://npm.io/package/lodash.debounce.md) ^4.0.8

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.3.2 (latest) — 2021-12-08
- 0.3.1 — 2021-08-16
- 0.3.0 — 2021-08-12
- 0.2.8 — 2021-08-11
- 0.2.7 — 2021-08-11
- 0.2.6 — 2021-08-11
- 0.2.5 — 2020-08-17
- 0.2.4 — 2020-08-17
- 0.2.3 — 2019-09-04
- 0.2.2 — 2019-08-29
- 0.2.1 — 2019-08-27
- 0.2.0 — 2019-08-20
- 0.1.0 — 2019-08-16
- 0.1.1-alpha — 2019-03-25
- 0.1.0-alpha — 2019-03-25

## README

# react-use-search

> 🔍 React hook for searching and filtering data

[![NPM](https://img.shields.io/npm/v/react-use-search.svg)](https://www.npmjs.com/package/react-use-search)
[![CircleCI](https://circleci.com/gh/prograsdk/react-use-search.svg?style=svg)](https://circleci.com/gh/prograsdk/react-use-search)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

This React hook aims to provide a simple way to implement search/filter functionality in React components.

## Install

```bash
yarn add react-use-search
```

## Usage

#### `useSearch<T>`

`react-use-search` provides the `useSearch` hook as named export; it takes three parameters:

 * `collection: T[]`
 An array of elements of type `T`.
 
 * `predicate:`[`Predicate<T>`](#predicatet):
 A boolean predicate function used for filtering elements in `collection` based on a query.
 
 * `options:`[`Options`](#options): 
 configuration object.

The hook returns a 4-tuple of type `[T[], string, React.ChangeEventHandler<HTMLInputElement>, (query: string) => void]` consisting of the following elements:

 * `T[]`:
 A filtered version of `collection` passed to `useSearch`.
 
 * `string`:
 The current query.
 
 * `React.ChangeEventHandler<HTMLInputElement>`:
 An event handler for an HTML input element.
 This is to be passes to the search input element as its `onChange` prop.
 
 * `(query: text) => void`:
 A function to programmatically set the query value

The example show a simple component listing users that can be searched by email.

```JSX
import React from 'react'
import {useSearch} from 'react-use-search'
import User from './User'

const predicate = (user, query) => user.email.includes(query)

const UserList = ({users}) => {
  const [filteredUsers, query, handleChange, setQuery] = useSearch(users, predicate, {debounce: 200})

  return (
    <div>
      <input placeholder="Search users by email..." value={query} onChange={handleChange} />
      {filteredUsers.map(user => <User key={user.id} {...user} />)}
      <button onClick={() => setQuery('@example')}>Search for @example addresses</button>
    </div>
  )
}
```

### Types

`react-use-search` exports the following types used to describe the parameteres of `useSearch`.

#### `Predicate<T>`

A binary boolean predicate function of type `(item: T, query: string) => boolean` used in `useSearch` to determine which elements of `collection` should be returned based on the current query.
The parameters of the function are:

 * `item: T`:
 The current element being evaluated.
 
 * `query: string`:
 The current query string.

```typescript
import {Predicate} from 'react-use-search'

interface User {
  email: string;
}

const predicate: Predicate<User> = (user, query) => user.email.includes(query)
```

#### `Options`

An options object used to configure `useSearch`.
It has the following properties:

 * `initialQuery?: string`: 
 The query used for the initial collection returned from `useSearch`.
 
 * `filter?: boolean`: 
 Determines if `useSearch` should behave in a _filtering_ or _searching_ like manner (according to [this definition on StackExchange](https://ux.stackexchange.com/a/4756)).
 If `true`, all elements in `collection` are returned if the current query is empty;
 if `false` (default), an empty array is returned.
 
 * `debounce?: number`:
 This option allows the internal filtering functionality of `useSearch` to be debounced.
 This can be advantageous in cases where `predicate` is complex or `collection` is large.

## License

MIT © [Progras ApS](https://github.com/prograsdk)

---

This hook is created using [create-react-hook](https://github.com/hermanya/create-react-hook).

---
_Source: https://npm.io/package/react-use-search · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
