with-xstate

0.0.6 • Public • Published

(HOC) With XState

Installation

npm install with-xstate
or
yarn add with-xstate

Require

Usage

In order to use withXState first pass the machine state through a prop to your component and then wrap it

mapActionsToXState((dispatch) => {...})

takes a function that should return and object that contains a key, value pair, where each key is a machine state and its value should be another object that contain a key, value pair with the actions and the functions that should be execute once xstate added to the next state.

NOTE mapActionsToXState pass dispatch as optionally prop so you can dispatch redux actions in your state actions.

import { Machine } from 'xstate';
import withXState from 'with-xstate';
import Component from './Component';
 
const toggleMachine = Machine({
  initial: 'inactive',
  states: {
    inactive: {
      on: {
        TOGGLE: 'active',
        onEntry: ['fetchQuery']
      }
    },
    active: { on: { TOGGLE: 'inactive' } }
  }
});
 
const mapActionsToXState = () => ({
  'machineState': {
    fetchQuery: () => {
      console.log('fetch...')
    }
  }
});
 
const WithXStateComponent = withXState(mapActionsToXState)(Component);
 
<WithXStateComponent machineState={machine} />

Dependencies (4)

Dev Dependencies (5)

Package Sidebar

Install

npm i with-xstate

Weekly Downloads

0

Version

0.0.6

License

MIT

Unpacked Size

11.9 kB

Total Files

6

Last publish

Collaborators

  • maximux13