React hook to search a Fuse.js index
npm install react-use-fusejsReact hook to search a Fuse.js index
> If you have plan to using this with gatsby-plugin-fusejs. please see gatsby-plugin-fusejs
``sh`
npm install --save react-use-fusejs
`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 (
#### Arguments
-
query: The search query
- data: The search data
- index: The Fuse index. see Fuse.js indexing
- fuseOpts: A object of Fuse.js options. see Fuse.js options
- parseOpts: A object of Fuse.parseIndex. see Fuse.parseIndex
- searchOpts: A object of Fuse.search$3
React hook for index data from
gatsby-plugin-fusejs> Please see gatsby-plugin-fusejs 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
) const [query, setQuery] = React.useState('')
const result = useGatsbyPluginFusejs(query, data.fusejs)
return (
type="text"
value={query}
onChange={(e) => setQuery(e.target.value)}
/>
{result.map(({ item }) => (
- {item.title}
))}
)
}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
- parseOpts: A object of Fuse.parseIndex. see Fuse.parseIndex
- searchOpts`: A object of Fuse.search