1.0.1 • Published 5 years ago

@klintm/phase-react v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

phase-react is the react plugin for phase.

Features

Exports a hook that allows subscribing to a phase store and path.

Installation

npm

npm i @klintm/phase-react

Usage

import Phase from "@klintm/phase";
import { useStore } from "@klintm/phase-react";

const counter = Phase(
  {
    name: "",
    count: 0
  },
  state => ({
    changeName: n => state.set("name", n),
    increment: () => state.set("count", state("count") + 1),
    decrement: () => state.set("count", state("count") - 1)
  })
);

const Counter = () => {
  const state = useStore(counter);
  const { decrement, increment } = state.actions;

  return (
    <div>
      <div>Count: {state("count")}</div>
      <button onClick={decrement}>-</button>
      <button onClick={increment}>+</button>
    </div>
  );
};