我想在反应应用程序中使用lunr js来搜索字符串,但我不知道如何设置 lunr 索引。在它说的文档中,The Lunr index. This can be an instance of a Lunr index or one that has been exported via JSON.stringify.
但我不知道这是如何完成的。
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>
)
}