Skip to content

Latest commit

 

History

History
21 lines (18 loc) · 659 Bytes

index.md

File metadata and controls

21 lines (18 loc) · 659 Bytes

Transition Effect Utility

The utility enables you to execute a callback before the store state transitions to another state. This utility is particularly useful for adding side effects to state machines.

import { createMachineStore, transitionEffect } from '@yobta/stores'

const store = createMachineStore({
  idle: ['fetching'],
  fetching: ['idle', 'error'],
  error: ['idle', 'fetching'],
})('idle')
let retryCount = 0
const unsubscribe = transitionEffect(store, 'fetching', () => {
  if (store.last() === 'error') {
    retryCount = retryCount + 1
  }
})
// Later when you need to:
unsubscribe()