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

jstates-react

v0.5.2

Published

JStates React - A subscribe function and a useSubscribe hook to use JStates state library

Downloads

30

Readme

JStates for React.js

A subscribe function to and a useSubscribe hook use JStates state library

NPM

GitHub issues license npm bundle size npm

Install

npm i -S jstates-react

Usage

Creating a state instance

// counterState.js
import { createState } from "jstates-react";

const counterState = createState({ counter: 0 });

export default counterState;

Making state changes

// Counter.jsx
import counterState from "./counterState";

function Counter() {
  return (
    <div>
      <button
        onClick={() => {
          counterState.setState(({ counter }) => ({ counter: counter + 1 }));
        }}
      >
        add one +
      </button>
      <button
        onClick={() => {
          counterState.setState(({ counter }) => ({ counter: counter - 1 }));
        }}
      >
        remove one -
      </button>
      <button
        onClick={() => {
          counterState.setState({ counter: 0 });
        }}
      >
        reset
      </button>
    </div>
  );
}
export default Counter;

Subscribing to state changes

// App.jsx with hooks
import { useSubscribe } from "jstates-react";
import counterState from "./counterState";
import Counter from "./Counter";

function App() {
  const { counter } = useSubscribe(counterState);
  return (
    <div>
      <p>Current counter: {counter}</p>
      <Counter />
    </div>
  );
}

export default App;
// App.jsx with HOC
import { subscribe } from "jstates-react";
import counterState from "./counterState";
import Counter from "./Counter";

function App({ states }) {
  return (
    <div>
      <p>Current counter: {states[0].counter}</p>
      <Counter />
    </div>
  );
}

export default subscribe(App, counterState);

useSubscribe hook

useSubscribe should be called with one jstates state instance. Then would get updated whatever changes are made to the state it subscribed to

const someState = createState({ userLikes: "nothing yet" });

function App() {
  const { userLikes } = useSubscribe(someState);

  return (
    <div>
      <p>You like: {userLikes}</p>
      <button onClick={() => someState.setState({ userLikes: "Monkeys" })}>
        Monkeys
      </button>
      <button onClick={() => someState.setState({ userLikes: "Horses" })}>
        Horses
      </button>
    </div>
  );
}

HOC subscribe

Minimal requirement

subscribe should be called with a component and a state or an array with at least one jstates state instance. Then would get updated whatever changes are made to the states it subscribed to

const someState = createState({});

subscribe(Component, someState);

// With multiple states
const someOtherState = createState({});

subscribe(Component, [someState, someOtherState]);

mapStatesToProps

subscribe can be called with an additional function to map the states it subscribes to into the props of the component. This pattern can be seen also in libraries like react-redux and is easy to test

const mapStatesToProps = (counterState, otherState) => ({
  counter: counterState.counter,
  someOtherValue: otherState.someOtherValue,
});

function App({ counter, someOtherValue }) {
  return (
    <div>
      <p>Current counter: {counter}</p>
      <p>Another value: {someOtherValue}</p>
      <Counter />
    </div>
  );
}

subscribe(App, [counterState, otherState], mapStatesToProps);