use-pro-state
v1.0.2
Published
use useState like a pro
Downloads
3
Readme
useProState
Install
$ yarn add use-pro-state
or
$ npm i use-pro-state
Usage
useProState(obj)
useProState
can accept an plain object or array, and return a delegated object or array. You can use it as what is passed into useProState
obj.$()
the delegated object has a function called $
, it will directly invoke the set function from useState
. So use it in the way you use the set function from useState
Example
import useProState from 'use-pro-state'
const App = () => {
const david = {
name: 'David',
pets: {
dogs: ['Leo', 'Lucky'],
cats: []
}
}
const davidPro = useProState(david)
return (
<div>
<p>{`${davidPro.name} lives with ${davidPro.pets.dogs.length + davidPro.pets.cats.length} pets`}</p>
<button
onClick={() => {
davidPro.pets.cats.push('Lisa')
davidPro.$()
}}
>
Add a cat
</button>
</div>
)
}
License
MIT