import { useState , useEffect , useRef , Dispatch , SetStateAction } from 'react' ; import Fuse from 'fuse.js' ; function useSearch < T > ( items , config ) { const [ search , setSearch ] = useState ( '' ) ; const [ filteredItems , setItems ] = useState ( items ) ; const indexer = useRef < any > ( null ) ; useEffect ( ( ) => { indexer . current = new Fuse ( items , config ) ; } , [ items , config ] ) ; useEffect ( ( ) => { if ( search === '' ) { setItems ( [ ] ) ; } else { setItems ( indexer . current . search ( search ) ) ; } } , [ search ] ) ; return [ filteredItems , setSearch ] ; } export default useSearch ;