Skip to content

Latest commit

 

History

History
64 lines (50 loc) · 1.15 KB

README.md

File metadata and controls

64 lines (50 loc) · 1.15 KB

usestore-ts - React state management library

Build Status npm

Installation

npm install usestore-ts

Configure TypeScript

Edit tsconfig.json to use decorators:

    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,

Example

import { Store, Action, useStore } from 'usestore-ts';

@Store()
class CounterStore {
  count = 0;

  @Action()
  increase() {
    this.count += 1;
  }

  @Action()
  reset() {
    this.count = 0;
  }
}

const counterStore = new CounterStore();

export default function Counter() {
  const [{ count }, store] = useStore(counterStore);

  return (
    <div>
      <p>
        Count:
        {' '}
        {count}
      </p>
      <p>
        <button type="button" onClick={() => store.increase()}>
          Increase
        </button>
        <button type="button" onClick={() => store.reset()}>
          Reset
        </button>
      </p>
    </div>
  )
}