npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@state-designer/react

v3.0.0

Published

State management with state-charts.

Downloads

815

Readme

State Designer / React

This package includes the core functions of State Designer, a JavaScript state management library, together with a React hook for use in React. Click here to learn more.

You can use this package in any React project. For other JavaScript projects, see @state-designer/core.

This package includes TypeScript typings.

Installation

npm install @state-designer/react
yarn add @state-designer/react

Usage

You can use the useStateDesigner hook to subscribe to either a global state (created with the createState function) or a local component state, by passing a configuration object to the hook. You can use the useSelector hook to subscribe to only part of a state.

For the basics of State Designer (including the configuration object API) see the documentation for @state-designer/core or visit state-designer.com.

API


useStateDesigner

const update = useStateDesigner(myState)

Subscribes a component to an state created with createState.

import { createState, useStateDesigner } from '@state-designer/react'

export const state = createState({
  data: { count: 0 },
  states: {
    enabled: {
      on: {
        INCREMENTED: { do: (data) => data.count++ },
        TOGGLED: { to: 'disabled' },
      },
    },
    disabled: {
      on: {
        TOGGLED: { to: 'enabled' },
      },
    },
  },
})

function Counter() {
  const local = useStateDesigner(state)
  return (
    <div>
      <button disabled={!state.can('INCREMENTED')} onClick={() => state.send('INCREMENTED')}>
        {local.data.count}
      </button>
      <button onClick={() => state.send('TOGGLED')}>Toggle</button>
    </div>
  )
}

Usage with a Configuration Object

const update = useStateDesigner(myState)

If given a configuration object, the hook will creates a new state and subscribe to it.

import { useStateDesigner } from '@state-designer/react'

function Counter() {
  const local = useStateDesigner({
    data: { count: 0 },
    states: {
      enabled: {
        on: {
          INCREMENTED: { do: (data) => data.count++ },
          TOGGLED: { to: 'disabled' },
        },
      },
      disabled: {
        on: {
          TOGGLED: { to: 'enabled' },
        },
      },
    },
  })

  return (
    <div>
      <button disabled={!state.can('INCREMENTED')} onClick={() => state.send('INCREMENTED')}>
        {local.data.count}
      </button>
      <button onClick={() => state.send('TOGGLED')}>Toggle</button>
    </div>
  )
}

Dependencies

When given a configration object, you may also provide an array of dependencies that will, if changed between renders, cause the component to create and subscribe to a new state based on the current configuration object.

import { useStateDesigner } from '@state-designer/react'

function Counter({ initial = 0 }) {
  const local = useStateDesigner(
    {
      data: { count: initial },
      on: { INCREMENTED: { do: (data) => data.count++ } },
    },
    [initial]
  )

  return (
    <div>
      <button onClick={() => state.send('INCREMENTED')}>{local.data.count}</button>
    </div>
  )
}

useSelector

const selected = useSelector(state, selectFn)

Subscribe to only certain changes from a state. Pass it a created state, together with a callback function that receives the update and returns some value. Whenever the state updates, the hook will update only if the value returned by your callback function has changed.

import { createState, useSelector } from '@state-designer/react'

const state = createState({
  data: {
    tables: 0,
    chairs: 0,
  },
  on: {
    ADDED_TABLE: (data) => data.tables++,
    ADDED_CHAIRS: (data) => data.chairs++,
  },
})

// This component will only update when state.data.tables changes.
function TablesCounter() {
  const tables = useSelector(state, (state) => state.data.tables)
  return <button onClick={() => state.send('ADDED_TABLE')}>{tables}</button>
}

// This component will only update when state.data.chairs changes.
function ChairsCounter() {
  const chairs = useSelector(state, (state) => state.data.chairs)
  return <button onClick={() => state.send('ADDED_CHAIR')}>{chairs}</button>
}

Comparisons

The hook will only update if the selector function returns new data. By default, the hook uses strict equality (a === b) to compare the previous data with the new data. However, the hook also accepts an optional third argument: a callback function that it will use for comparisons instead.

Each time the state updates, the hook will use the selection function to make a new selection, then call the comparison function with two arguments: the selection currently in state (the "previous" selection) and the new selection just obtained. If the comparison function returns false, then the hook will put the new selection into state.

const tooMany = useSelector(
  state,
  (state) => state.data.chairs,
  (prev, next) => prev > 10 !== next > 10
)

In the example above, the hook will only change if the state's data.chairs property is below 10 and changes to above 10, or if it is above 10 and changes to below 10. A change from 4 to 11 would cause the state to update; a change from 11 to 12 would not.


createSelectorHook

createSelectorHook(state)

This is a helper that will generate a selector hook from a state. The hook returned by createSelectorHook is unlike the useSelector shown above in that you will not have to enter the state as a first argument.

import { createState, createSelectorHook } from '@state-designer/react'

const state = createState({
  data: {
    tables: 0,
    chairs: 0,
  },
  on: {
    ADDED_TABLE: (data) => data.tables++,
    ADDED_CHAIRS: (data) => data.chairs++,
  },
})

const useSelector = createSelectorHook(state)

function TablesCounter() {
  const tables = useSelector((state) => state.data.tables)
  return <button onClick={() => state.send('ADDED_TABLE')}>{tables}</button>
}