All Projects → francisrstokes → React-Machinery

francisrstokes / React-Machinery

Licence: MIT license
🔥 React Machinery provides a simple to use, component based approach to state machines in react.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React-Machinery

tstate-machine
TypeScript implementation of State Manager(like StateMachine)
Stars: ✭ 20 (-80.77%)
Mutual labels:  state-machine, state
xstate-viz
Visualizer for XState machines
Stars: ✭ 274 (+163.46%)
Mutual labels:  state-machine, state
xstate
State machines and statecharts for the modern web.
Stars: ✭ 21,286 (+20367.31%)
Mutual labels:  state-machine, state
StateBuilder
State machine code generator for C++ and Java.
Stars: ✭ 30 (-71.15%)
Mutual labels:  state-machine, state
Little State Machine
📠 React custom hook for persist state management
Stars: ✭ 654 (+528.85%)
Mutual labels:  state-machine, state
SimpleStateMachineLibrary
📚 A simple library for realization state machines in C# code
Stars: ✭ 30 (-71.15%)
Mutual labels:  state-machine, state
statemachine-go
🚦 Declarative Finite-State Machines in Go
Stars: ✭ 47 (-54.81%)
Mutual labels:  state-machine, state
Finity
A finite state machine library for Node.js and the browser with a friendly configuration DSL.
Stars: ✭ 88 (-15.38%)
Mutual labels:  state-machine, state
Machinery
State machine thin layer for structs (+ GUI for Phoenix apps)
Stars: ✭ 367 (+252.88%)
Mutual labels:  state-machine, state
aper
A Rust data structure library built on state machines.
Stars: ✭ 100 (-3.85%)
Mutual labels:  state-machine, state
Hal
🔴 A non-deterministic finite-state machine for Android & JVM that won't let you down
Stars: ✭ 63 (-39.42%)
Mutual labels:  state-machine, state
Xstate
State machines and statecharts for the modern web.
Stars: ✭ 18,300 (+17496.15%)
Mutual labels:  state-machine, state
Reamp
A painkiller for your Android apps
Stars: ✭ 51 (-50.96%)
Mutual labels:  state
react-native-easy-state-view
Fully customizable State View for React Native.
Stars: ✭ 21 (-79.81%)
Mutual labels:  state
enform
Handle React forms with joy 🍿
Stars: ✭ 38 (-63.46%)
Mutual labels:  state
react-globally
Gives you setGlobalState, so you can set state globally
Stars: ✭ 22 (-78.85%)
Mutual labels:  state
react-redux-modals
This repo created for Medium.com: React/Redux: Modals and Dialogs
Stars: ✭ 30 (-71.15%)
Mutual labels:  state
react-state
React-State - Superpowers for managing local and reusable state in React
Stars: ✭ 14 (-86.54%)
Mutual labels:  state
hyperapp-example
hyperapp example
Stars: ✭ 14 (-86.54%)
Mutual labels:  state
oh-my-design-patterns
🎨 Record the articles and code I wrote while learning design patterns
Stars: ✭ 33 (-68.27%)
Mutual labels:  state

React Machinery

npm version license CircleCI

⚙️ State Machine Modelling for React

Description

🔥 React Machinery provides a simple to use, component based approach to state machines in react.

Describe your states, transitions, and which component each renders, and plug it into the StateMachine component. It accepts two extra functions; one for getting the current state name and one for setting it. This allows your app to flexibly use and swap out different ways of storing data - be it in component state, redux, mobx, whatever.

Examples

Examples of how the following state diagram can be implemented in both vanilla react and using redux can be found in the examples folder.

State diagram of code below

Installation

# with yarn
yarn add react-machinery

# or with npm
npm i react-machinery

API

StateMachine

All props for the StateMachine component are required.

getCurrentState

function()

A function returning the current state name, stored somewhere like a react component state, or in redux.

setNewState

function(newStateName)

A function that updates the current state, stored somewhere like a react component state, or in redux.

states

Array of state definitions

A state definition is a plain javascript object with the following properties:

{
  // This name corresponds to the one coming from getCurrentState() and
  // being set by setNewState()
  name: 'State Name',

  // Array of plain objects that describe automatic transitions
  // These are evaluated after a transition and when props change
  autoTransitions: [
    // A transition object must contain two properties:
    // test, which is a function that recieves the StateMachine data, and returns true if a state change should take place
    // newState, which is the name of the state to transition to when the test function returns true
    {
      test: data => data === 'expected for state change',
      newState: 'Name of new state'
    }
  ],

  // This is a list of states that can be transitioned to from the current state using the transitionTo
  // prop passed to a state component. Trying to use transitionTo with a state not specified in this list
  // will throw an error. This list, together with any 'newState's described in autoTransitions form the
  // full set of valid transitions for this state.
  validTransitions: [
    'Name of valid state'
  ],

  // beforeRender is an optional function that can run some code before this states component
  // is rendered. For anything sufficiently complex however, it's better to use a react class
  // component and take advantage of lifecycle hooks
  beforeRender: (data) => {
    data.startAPIRequest();
  },

    // One of the following two properties must be implemented:

  // a render prop that recieves the 'props' object supplied to the StateMachine
  // the props object will also include a 'transitionTo' function and a 'currentState' string
  render: (data) => {
    return <SomeComponent propUsingStateName={data.currentState} {...data} />
  },

  // Or just a regular react component
  component: SomeReactComponent
}

data

object

An object contains data that defines all the states in the state machine. This data is supplied to the component rendered by any state, to test functions in autoTransitions. If a render prop is used for the state, then the props are passed as the argument, along with a transitionTo function and currentState name.

Logo

The awesome logo was designed by @ayushs08, who graciously provided under CC BY 3.0. Many thanks!

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].