MiniSearch 用戶端全文搜尋庫的 React 整合。
首先,請確保您安裝了 React 和 MiniSearch 的兼容版本。
然後,透過NPM或Yarn安裝:
# With NPM:
npm install react-minisearch
# Or with Yarn:
yarn add react-minisearch
使用react-minisearch
主要有三種方式: useMiniSearch
鉤子、 withMiniSearch
高階元件或WithMiniSearch
包裝組件。
所有三種方式都採用以下參數(或包裝器組件的道具):
add
/ addAll
/ remove
/ removeAll
/ discard
等。MiniSearch
配置選項 import { useMiniSearch } from 'react-minisearch'
// Documents to search amongst
const documents = [
{ id : 1 , name : 'Agata' } ,
{ id : 2 , name : 'Finn' } ,
// …etc
]
// See MiniSearch for documentation on options
const miniSearchOptions = { fields : [ 'name' ] }
const MyComponent = ( ) => {
const { search , searchResults } = useMiniSearch ( documents , miniSearchOptions )
const handleSearchChange = ( event ) => {
search ( event . target . value )
}
return (
< div >
< input type = 'text' onChange = { handleSearchChange } placeholder = 'Search…' / >
< ol >
< h3 > Results: < / h3 >
{
searchResults && searchResults . map ( ( result , i ) => {
return < li key = { i } > { result . name } < / li >
} )
}
< / ol >
< / div >
)
}
import { withMiniSearch } from 'react-minisearch'
const MyComponent = ( { search , searchResults } ) => {
const handleSearchChange = ( event ) => {
search ( event . target . value )
}
return (
< div >
< input type = 'text' onChange = { handleSearchChange } placeholder = 'Search…' / >
< ol >
< h3 > Results: < / h3 >
{
searchResults && searchResults . map ( ( result , i ) => {
return < li key = { i } > { result . name } < / li >
} )
}
< / ol >
< / div >
)
}
// Documents to search amongst
const documents = [
{ id : 1 , name : 'Agata' } ,
{ id : 2 , name : 'Finn' } ,
// …etc
]
// See MiniSearch for documentation on options
const miniSearchOptions = { fields : [ 'name' ] }
const MyComponentWithSearch = withMiniSearch ( documents , miniSearchOptions , MyComponent )
import { WithMiniSearch } from 'react-minisearch'
// Documents to search amongst
const documents = [
{ id : 1 , name : 'Agata' } ,
{ id : 2 , name : 'Finn' } ,
// …etc
]
// See MiniSearch for documentation on options
const miniSearchOptions = { fields : [ 'name' ] }
const MyComponent = ( ) => (
< WithMiniSearch documents = { documents } options = { miniSearchOptions } >
{
( { search , searchResults } ) => {
const handleSearchChange = ( event ) => {
search ( event . target . value )
}
return (
< div >
< input type = 'text' onChange = { handleSearchChange } placeholder = 'Search…' / >
< ol >
< h3 > Results: < / h3 >
{
searchResults && searchResults . map ( ( result , i ) => {
return < li key = { i } > { result . name } < / li >
} )
}
< / ol >
< / div >
)
}
}
< / WithMiniSearch >
)
對於所有三種方式( useMiniSearch
、 withMiniSearch
或WithMiniSearch
), react-minisearch
提供的完整 props 集都是相同的:
search(query: string, searchOptions?: SearchOptions) => void
:為了執行搜尋而呼叫的函數
searchResults: T[] | null
:搜尋結果數組,如果未執行搜索或清除搜索,則為null
rawResults: SearchResult[] | null
:MiniSearch 的原始搜尋結果數組,包括分數和匹配訊息,或未執行搜尋或清除搜尋時null
clearSearch() => void
:為了清除搜尋而呼叫的函式(將searchResults
設為null
)
autoSuggest(query: string, searchOptions?: SearchOptions) => void
:為了取得自動建議而呼叫的函數
suggestions: Suggestion[] | null
:自動建議數組,未使用或清除自動建議時為null
clearSuggestions() => void
:為了清除建議而呼叫的函數(將suggestions
設為null
)
add(document: T) => void
:將新文件新增至索引的函數
addAll(documents: T[]) => void
:將多個新文件批次新增至索引的函數
addAllAsync(documents: T[], options?: object) => Promise<void>
:與addAll
相同,但非同步且批次工作以避免阻塞 UI
remove(document: T) => void
:從索引中刪除文件的函數
removeById(id: any) => void
:透過 ID 從索引中刪除文件的函數
removeAll(documents?: T[]) => void
:從索引中刪除多個文件或所有文件的函數
discard(id: any) => void
:按 ID 丟棄文件(與removeById
相同)
discardAll(ids: readonly any[]) => void
:按 ID 一次丟棄多個文檔
replace(document: T) => void
:以新版本取代現有文檔
isIndexing: boolean
: 透過addAllAsync
進行索引時設為true
,否則設為false
miniSearch: MiniSearch
: MiniSearch
實例,用於需要直接使用它的(罕見)情況
在此清單中,類型T
是泛型類型,指的是正在索引的文件的類型。
其中許多 props 對應於MiniSearch
類別的方法,如 MiniSearch 庫中所述。
查看examples
目錄以取得完整的使用範例。要在本地運行範例應用程式:
cd
到examples
目錄yarn install
(或npm install
)安裝依賴項yarn start
(或npm run start
)運行範例應用程式