1.0.0 • Published 6 years ago

react-easy-hooks v1.0.0

Weekly downloads
1
License
MIT*
Repository
-
Last release
6 years ago

🎣 react-easy-hooks

package version package downloads standard-readme compliant package license make a pull request

Collection of useful React hooks

Table of Contents

Install

This project uses node and npm.

$ npm install react-easy-hooks
$ # OR
$ yarn add react-easy-hooks

API

Table of Contents

useInput

Hook for input values

Parameters

  • initial Any Initial input value (optional, default '')
  • validation Function Validation function (optional, default ()=>true)

Examples

import { useInput } from 'react-easy-hooks'

const Component = () => {
    const name = useInput('Tiaan', (val) => val.length > 3)
    return <div>
         {!name.valid && 'Invalid'}
         <input {...name}/>
     </div>
}

useOnMount

Hook to call on component unmount

Parameters

Examples

import { useOnMount } from 'react-easy-hooks'

const Component = () => {
     useOnMount(() => { console.log('Mounted!') })
     return <h1>Hi</h1>
}

useOnUnmount

Hook to call on component unmount

Parameters

Examples

import { useOnUnmount } from 'react-easy-hooks'

const Component = () => {
     useOnUnmount(() => { console.log('Unmounted!') })
     return <h1>Hi</h1>
}

usePrev

Hook to get previous value of a prop or a state value. Straight out of React docs

Parameters

  • value Any

useToggle

Parameters

  • initial Boolean Initial value of toggle (optional, default false)

Examples

import { useToggle } from 'react-easy-hooks'

const Component = () => {
   const name = useToggle()

   return <div>
         <button onClick={name.toggle}>Toggle</button>
         {name.isOn && 'Tiaan'}
     </div>
}

Contribute

  1. Fork it and create your feature branch: git checkout -b my-new-feature
  2. Commit your changes: git commit -am 'Add some feature'
  3. Push to the branch: git push origin my-new-feature
  4. Submit a pull request

License

MIT