Skip to content

terzhang/useStateBot

Repository files navigation

useStateBot

A finite state machine React hook

NPM JavaScript Style Guide Travis-CI

Install

npm install --save usestatebot

or

yarn add usestatebot

Usage

import React from 'react';

import useStateBot from 'usestatebot';

const App = () => {
  const stateBot = useStateBot({
    initialState: 'idle',
    idle: { to: 'loading' },
    loading: { to: 'ready' },
    ready: {
      onEnter() {
        return console.log('I am ready');
      },
    },
  });

  return (
    <div>
      {stateBot.getState()}
      <button onClick={() => stateBot.next()}>Next State</button>
    </div>
  );
};

License

MIT © terzhang