# react-use-fusejs

> React hook to search a Fuse.js index

Latest version **2.0.2** (published 2023-05-05) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2023-05-05 |
| First published | 2022-07-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 13.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 11 |
| Author | johnny-mh |
| Maintainers | everedifice |
| Keywords | react, search, fuse.js |

## Links

- npm: https://www.npmjs.com/package/react-use-fusejs
- Repository: https://github.com/johnny-mh/blog2
- Homepage: https://github.com/johnny-mh/blog2/tree/main/packages/react-use-fusejs
- Issues: https://github.com/johnny-mh/blog2/issues
- npm.io page: https://npm.io/package/react-use-fusejs

## Dependencies (2)

- [react](https://npm.io/package/react.md) ^18.0.0
- [fuse.js](https://npm.io/package/fuse.js.md) ^6.0.0

## 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

- 2.0.2 (latest) — 2023-05-05
- 2.0.1 — 2023-03-17
- 2.0.0 — 2023-01-24
- 1.0.1 — 2022-07-05
- 1.0.0 — 2022-07-04

## README

# react-use-fusejs

React hook to search a [Fuse.js](https://fusejs.io/) index

> If you have plan to using this with `gatsby-plugin-fusejs`. please see [gatsby-plugin-fusejs](https://github.com/johnny-mh/blog2/tree/main/libs/gatsby-plugin-fusejs#readme)

## Install

```sh
npm install --save react-use-fusejs
```

## How to use

### `useFusejs`

```js
import Fuse from 'fuse.js'
import useFusejs from 'react-use-fusejs'

const books = [
  {
    title: "Old Man's War",
    author: {
      firstName: 'John',
      lastName: 'Scalzi',
    },
  },
  {
    title: 'The Lock Artist',
    author: {
      firstName: 'Steve',
      lastName: 'Hamilton',
    },
  },
]

// Create the Fuse index
const myIndex = Fuse.createIndex(['title', 'author.firstName'], books)

export function Search() {
  const [query, setQuery] = useState('')
  const results = useFusejs(query, books, myIndex)

  return (
    <div>
      <input
        type="text"
        value={query}
        onChange={(e) => setQuery(e.target.value)}
      />
      <ul>
        {results.map((result) => (
          <li key={result.id}>
            {result.title} by {result.author.firstName} {result.author.lastName}
          </li>
        ))}
      </ul>
    </div>
  )
}
```

#### Arguments

- `query`: The search query
- `data`: The search data
- `index`: The Fuse index. see [Fuse.js indexing](https://fusejs.io/api/indexing.html)
- `fuseOpts`: A object of Fuse.js options. see [Fuse.js options](https://fusejs.io/api/options.html)
- `parseOpts`: A object of Fuse.parseIndex. see [Fuse.parseIndex](https://fusejs.io/api/parsing.html#fuse-parseindex)
- `searchOpts`: A object of Fuse.search

### `useGatsbyPluginFusejs`

React hook for index data from `gatsby-plugin-fusejs`

> Please see [gatsby-plugin-fusejs](https://github.com/johnny-mh/blog2/tree/main/libs/gatsby-plugin-fusejs#readme) to read more details

```jsx
import { useStaticQuery, graphql } from 'gatsby'
import * as React from 'react'
import { useGatsbyPluginFusejs } from 'react-use-fusejs'

export function Search() {
  const data = useStaticQuery(graphql`
    {
      fusejs {
        index
        data
      }
    }
  `)

  const [query, setQuery] = React.useState('')
  const result = useGatsbyPluginFusejs(query, data.fusejs)

  return (
    <div>
      <input
        type="text"
        value={query}
        onChange={(e) => setQuery(e.target.value)}
      />
      <ul>
        {result.map(({ item }) => (
          <li key={item.id}>{item.title}</li>
        ))}
      </ul>
    </div>
  )
}

export default Search
```

#### Arguments

- `query`: The search query
- `data`: A object from `gatsby-plugin-fusejs` plugin (You don't need processing that. just pass to this hook)
- `fuseOpts`: A object of Fuse.js options. see [Fuse.js options](https://fusejs.io/api/options.html)
- `parseOpts`: A object of Fuse.parseIndex. see [Fuse.parseIndex](https://fusejs.io/api/parsing.html#fuse-parseindex)
- `searchOpts`: A object of Fuse.search

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