0.3.0 • Published 4 months ago

react-search-hook v0.3.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
4 months ago

Introduction

React Search Hook is a lightweight library for React, with which you can store the search text in the Store and use it in other components.

Visit online demo

It is production-ready, and gives you the following:

  • 📦 Very lightweight
  • 🔧 Easy to use
  • ⚛️ Build for React JS
  • ⌨️ Highly typed and written in TypeScript

Documentation

1. Install

npm

npm i react-search-hook

yarn

yarn add react-search-hook

CDN

2. Add provider to top of your component tree

import { SearchProvider } from 'react-search-hook'
function App() {
  return <SearchProvider stores={['products',...]}>{children}</SearchProvider>
}

notice that SearchProvider needs an array of strings to make stores

3. Simply you can import useSearch hook everywere

import { useSearch } from 'react-search-hook'
function MyExampleComponent() {
  const { register } = useSearch('products')

  return (
    <div>
      <input {...register()} />
      <span>{search}</span>
    </div>
  )
}

notice that useSearch needs the store name

If you need to filter some array of data, simply you can pass items to useSearch hook

import { useSearch } from 'react-search-hook'
function MyExampleComponent() {
  const items = ['text1', 'text2', ...]
  const { register, searchResult } = useSearch('products', { items })

  return (
    <div>
      <input {...register()} />
      {searchResult.map((item, key) => (
        <li>{JSON.stringify(item)}</li>
      ))}
    </div>
  )
}

APIs

useSearch(name,options)

NameTypeRequiredDescription
namestringyesThe name of store
optionsobject UseSearchOptionsnoPay attention to the table below

useSearch options*

NameTypeRequiredDescription
itemsarray of strings or objectsThe array of strings or objects to be filtered
searchPropstringyes if each item is objectIf each item is an object, it specifies the desired property of the filter

useSearch responses

NameTypeDescription
seachstringThe current value of the specified store
setSearchfunctionfunction that updates the specified store
registerfunctionThis function returns an object with properties required for registering an input
searchResultarray of itemsIf options include items (and a search property for array of objects), items will filter with search value

Contributor ✨

Contributors

0.3.0

4 months ago

0.2.0

4 months ago

0.1.0

5 months ago

0.0.4

5 months ago

0.0.3

5 months ago

0.0.25

5 months ago

0.0.2

5 months ago

0.0.1

5 months ago