use-pro-state
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

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

Package Sidebar

Install

npm i use-pro-state

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

4.04 kB

Total Files

5

Last publish

Collaborators

  • fi3ework