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

@bemedev/x-interpret-react

v0.2.2

Published

A simple function that use XState like a RXjs Observable

Downloads

24

Readme

React XState interpreter

A simple function that use XState like a RXjs Observable.

Short introduction

Xstate

XState is a implementation of Statecharts, a graphical way to represent complexity in code.

Problems

  1. Complexity of React context :

When hooks were introduced, everyone was very happy. But with useState and useReducer, you cannot share data between components. So you need another function : createContext. But it cames with some complexity. So the “createContext” function returns a component named Provider. You need to wrap all components inside the Provider so you can use another hook “useContext”. Again three steps to share just a variable. It appears that it add more complexity than solutions.

  1. Recommend way to use XState globally :
  • In XState docs, it’s recommended to use react context to have a global service shareable between components. The procedure is tedious but it works like a charm.
  • Statecharts are usually big and has can have compound states, with many events. So it’s unusual to use it only in one component.
  1. Version 13 of Next.js :

The new app directory contains only server components by default. To use react context, you need to mark the component as client component. So the upper in the stack a client component is added, the harder you lose the benefits of SSR.

The solution

Just a simple function reactInterpret. It’s a hook creator and function generator.

It accepts the same arguments as XState function interpret.

It returns some functions :

{
  "start": "Start the interpreter (The same as Xstate Interpreter)",
  "stop": "Stop the interpreter (The same as Xstate Interpreter)",
  "send": "Send Event to the machine (The same as Xstate Interpreter)",
  "sender": {
    "definition": "A new way to simplify sending events",
    "example": {
      "event": {
        "type": "INPUT",
        "input": "string"
      },
      "prepareFunction": "const mySend = sender('INPUT')",
      "usage": "mySend({ input: 'name' })"
    }
  },
  "createSelector": "Create selector for hooks"
  /* ... */
}

, and hooks :

{
  "useSelector": "Select an element inside the current state (The same as Xstate hook but without the last parameter).",
  "useMatches": "Macther for the value of the current state (not from XState, derived from @bemedev/x-matches).",
  "useHasTags": "Macther for tags of current value. Accept param array."
}

NB : Check the Library : @bemedev/x-matches to see how useMatches works.

Live documentation here

Enjoy your function ✌🏾😎 !