All Projects → mjstahl → use-state-machine

mjstahl / use-state-machine

Licence: MIT license
Use Finite State Machines with React Hooks

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to use-state-machine

xstate
State machines and statecharts for the modern web.
Stars: ✭ 21,286 (+75921.43%)
Mutual labels:  state-management, fsm, state-machine, finite-state-machine
Fsm As Promised
A finite state machine library using ES6 promises
Stars: ✭ 446 (+1492.86%)
Mutual labels:  fsm, state-machine, finite-state-machine
stateless
Finite State Machine porting from Stateless C#
Stars: ✭ 25 (-10.71%)
Mutual labels:  fsm, state-machine, finite-state-machine
Afsm
C++14 Finite State Machine library
Stars: ✭ 113 (+303.57%)
Mutual labels:  fsm, state-machine, finite-state-machine
UnityHFSM
A simple yet powerful class based hierarchical finite state machine for Unity3D
Stars: ✭ 243 (+767.86%)
Mutual labels:  fsm, state-machine, finite-state-machine
statemachine-go
🚦 Declarative Finite-State Machines in Go
Stars: ✭ 47 (+67.86%)
Mutual labels:  fsm, state-machine, finite-state-machine
Finity
A finite state machine library for Node.js and the browser with a friendly configuration DSL.
Stars: ✭ 88 (+214.29%)
Mutual labels:  fsm, state-machine, finite-state-machine
Jstate
Advanced state machines in Java.
Stars: ✭ 84 (+200%)
Mutual labels:  fsm, state-machine, finite-state-machine
Nanostate
🚦- Small Finite State Machines
Stars: ✭ 151 (+439.29%)
Mutual labels:  fsm, state-machine, finite-state-machine
FiniteStateMachine
This project is a finite state machine designed to be used in games.
Stars: ✭ 45 (+60.71%)
Mutual labels:  fsm, state-machine, finite-state-machine
Fluent State Machine
Fluent API for creating state machines in C#
Stars: ✭ 195 (+596.43%)
Mutual labels:  fsm, state-machine, finite-state-machine
simple-state-machine
A simple Java state machine for Spring Boot projects
Stars: ✭ 25 (-10.71%)
Mutual labels:  fsm, state-machine, finite-state-machine
pastafarian
A tiny event-based finite state machine
Stars: ✭ 20 (-28.57%)
Mutual labels:  fsm, state-machine, finite-state-machine
Statecharts.github.io
There is no state but what we make. Feel free to pitch in.
Stars: ✭ 265 (+846.43%)
Mutual labels:  fsm, state-machine, finite-state-machine
Django Fsm
Django friendly finite state machine support
Stars: ✭ 1,898 (+6678.57%)
Mutual labels:  fsm, state-machine, finite-state-machine
kstatemachine
KStateMachine is a Kotlin DSL library for creating finite state machines (FSM) and hierarchical state machines (HSM).
Stars: ✭ 63 (+125%)
Mutual labels:  state-management, fsm, state-machine
xstate-cpp-generator
C++ State Machine generator for Xstate
Stars: ✭ 33 (+17.86%)
Mutual labels:  state-management, fsm, state-machine
use-secret-code
Custom hook for adding cheat codes to your React app.
Stars: ✭ 16 (-42.86%)
Mutual labels:  state-machine, react-hooks
go-sm
A finite-state machine library for the Go programming language
Stars: ✭ 14 (-50%)
Mutual labels:  state-machine, finite-state-machine
asyncmachine
Relational State Machine with a visual inspector
Stars: ✭ 67 (+139.29%)
Mutual labels:  state-management, state-machine

npm bundle size Build Status JavaScript Style Guide

use-state-machine

Use Finite State Machines with React Hooks

Installation

$ npm install --save use-state-machine

Example

// H2O.js
import React from 'react'
import { useStateMachine } from 'use-state-machine'
import H2OState from './H2O.state'

function H2O () {
  const [current, transition] = useStateMachine(H2OState)
  return (
    <div>
      <p>Your H2O is in a {current.state} state.</p>
      <p>The temperature of your H2O is {current.value}.</p>
      <button
        disabled={!transition.toLiquid}
        onClick={() => transition.toLiquid()}>
        To Liquid
      </button>
      <button
        disabled={!transition.toSolid}
        onClick={() => transition.freeze()}>
        To Solid
      </button>
      <button
        disabled={!transition.toGas}
        onClick={() => transition.boil()}>
        To Gas
      </button>
    </div>
  )
}
// H2O.state.js
import { StateMachine } from 'use-state-machine'

export default new StateMachine({
  initial: 'liquid',
  liquid: {
    freeze: 'solid',
    boil: 'gas',
    value: '60F'
  },
  solid: {
    melt: 'liquid',
    value: '32F'
  },
  gas: {
    chill: 'liquid'
    value: '212F'
  }
})

API

useStateMachine

import { useStateMachine } from 'use-state-machine'

useStateMachine(machine: Object | StateMachine) -> [Object, Object]

useStateMachine takes a JavaScript object or StateMachine Object as an argument and returns an array consisting of a current object and a transition object.

current -> Object

The current state consists of two properties: state and value. state returns the string representing the current state. value returns the value (object or primitive) of the current state if one exists and returns undefined if not.

const [ current ] = useStateMachine(H2OState)

current.state //-> 'liquid'
current.value //-> '60F'

transition -> Object

transition is an object with a collection of functions allowing the developer to avoid transitioning using the string names. In the example above, when in the liquid state, two passive and two active functions exist on transition. The passive functions are transition.toSolid, transition.toGas. The two active functions are transition.freeze and transition.boil. All state specific functions on transition accept a single value argument.

If the value argument is an Object, the state's value and value argument will be merged. If the the state's value is not an Object, the state's value will be replaced with the value argument. If the state's value is a primitive and the value argument is an object, the state's value will be set to the value argument including a property named value set to the state's previous primitive value.

const [ current, transition ] = useStateMachine(H2OState)
transition.freeze()

current.state //-> 'solid'
current.value //-> '32F'

transition.melt()

current.state //-> 'liquid'

transition.toGas()

StateMachine

import { StateMachine } from 'use-state-machine'

new StateMachine(states: Object) -> StateMachine

To create an instance of a StateMachine pass a 'states' object. A valid 'states' object must have, at a minimum, a single state. And an initial property which is set to a valid state property.

There are two types of StateMachine definitions: "active" and passive. If the definition includes names for each valid transition it is an "active" definition and the transition property will include "active" functions (like freeze() and boild()). An example of an "active" definition is:

new StateMachine({
  initial: 'liquid',
  liquid: {
    freeze: 'solid',
    boil: 'gas',
    value: '60F'
  },
  solid: {
    melt: 'liquid',
    value: '32F'
  },
  gas: {
    chill: 'liquid'
    value: '212F'
  }
})

A "passive" definition uses the to property on each state indicating one or more valid states the current state can transition to. For a "passive" definition, the transition property will only include "passive" functions (like toSolid and toGas). An example of an "passive" definition is:

new StateMachine({
  initial: 'liquid',
  liquid: {
    to: ['solid', 'gas']
    value: '60F'
  },
  solid: {
    to: 'liquid'
    value: '32F'
  },
  gas: {
    to: 'liquid'
    value: '212F'
  }
})

<StateMachine>.state -> String

Return the string name of the StateMachine state.

<StateMachine>.value -> Any

value returns the value (object or primitive) of the current state if one exists and returns undefined if not.

<StateMachine>.transition -> Object

transition is an object with a collection of functions allowing the developer to avoid transitioning using the string names. In the example above, when in the liquid state, two passive and two active functions exist on transition. The passive functions are transition.toSolid, transition.toGas. The two active functions are transition.freeze and transition.boil. All state specific functions on transition accept a single value argument.

If the value argument is an Object, the state's value and value argument will be merged. If the the state's value is not an Object, the state's value will be replaced with the value argument. If the state's value is a primitive and the value argument is an object, the state's value will be set to the value argument including a property named value set to the state's previous primitive value.

<StateMachine>.onTransition(callback: Function) -> unsubscribe: Function

When a StateMachine object transitions from one state to another all callbacks passed to the onTransition function are evaluated with the StateMachine object passed as the only argument to the callback. onTransition returns a function that unsubscribes the callback when executed.

Maintainers

  • Mark Stahl

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].