use-object-state

0.0.3 • Public • Published

use-object-state

Brings your beloved behaviour of merging state object into old one when setting it to react 16.7.0 and beyond by providing useObjectState custom hook.

Example

import { useObjectState } from 'use-object-state';

export function CountersComponent() {
  const [counters, setCounters] = useObjectState({
    foo: 0,
    bar: 0,
  });
  
  const incrementFooCounter = () => setCounters({
    foo: counters.foo + 1,
  });
  const incrementBarCounter = () => setCounters({
    foo: counters.bar + 1,
  });
  
  return (
    <>
      Foo: {counters.foo},
      <button onClick={incrementFooCounter}>
        Increment foo
      </button>
      
      <br/>
      
      Bar: {counters.bar},
      <button onClick={incrementBarCounter}>
        Increment bar
      </button>
    </>
  );
}

Package Sidebar

Install

npm i use-object-state

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

2.71 kB

Total Files

4

Last publish

Collaborators

  • thers