🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

react-lunr

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-lunr

React component to search a Lunr index

1.1.0
latest
Source
npm
Version published
Weekly downloads
1.1K
31.01%
Maintainers
1
Weekly downloads
 
Created
Source

react-lunr

React hook to search a Lunr index.

Status

npm version Build Status

Installation

npm install --save react-lunr

API

useLunr

useLunr(query?: string | lunr.Index.QueryBuilder, index?: lunr.Index | JSON | string, store: object | string) => object[]

The useLunr hook takes your search query, index, and store and returns results as an array. Searches are memoized to ensure efficient searching.

Parameters

NameTypeDescription
querystring | lunr.Index.QueryBuilderThe search query. As this value updates, the return value will be updated. This can be a string or a query builder.
indexlunr.Index | JSON | stringThe Lunr index. This can be an instance of a Lunr index or one that has been exported via index.toJSON or JSON.stringify.
storeJSON | stringObject mapping a result ref to an object of data. This can be an object or an object that has been exported via JSON.stringified.

If store is not provided, the raw results data from Lunr will be returned instead. This includes the ref used when creating the index.

Example

The following example renders a text input and queries the Lunr index on form submission.

Note: Formik is used in the following example to handle form state, but is not required. As long as your query is passed as the first parameter, you can manage how to store it.

import React, { useState } from 'react'
import { useLunr } from 'react-lunr'
import { Formik, Form, Field } from 'formik'

const index = /* a lunr index */
const store = {
  1: { id: 1, title: 'Document 1' },
  2: { id: 2, title: 'Document 2' },
  3: { id: 3, title: 'Document 3' },
}

const SearchBar = () => {
  const [query, setQuery] = useState(null)
  const results = useLunr(query, index, store)

  return (
    <Formik
      initialValues={{ query: '' }}
      onSubmit={(values, { setSubmitting }) => {
        setQuery(values.query)
        setSubmitting(false)
      }}
    >
      <Form>
        <Field name="query" />
      </Form>
      <h1>Results</h1>
      <ul>
        {results.map(result => (
          <li key={result.id}>{result.title}</li>
        ))}
      </ul>
    </Formik>
  )
}

Keywords

react

FAQs

Package last updated on 06 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts