1.0.0 • Published 6 years ago
react-easy-hooks v1.0.0
🎣 react-easy-hooks
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
func
Function Function to call
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
func
Function Function to call
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, defaultfalse
)
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
- Fork it and create your feature branch: git checkout -b my-new-feature
- Commit your changes: git commit -am 'Add some feature'
- Push to the branch: git push origin my-new-feature
- Submit a pull request
License
MIT
1.0.0
6 years ago