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

react-mono-state

v0.6.1

Published

State Management Lib - reactive and less boilerplate

Downloads

7

Readme

react-mono-state

State Management Lib - reactive and less boilerplate. No need external middleware to perform state change asynchronously and you can handle any actions on any components and also make effects on it by using hooks - useActionHandler, useStoreEffect. Remaining useful hooks are - useSelector, useDispatch, useStore etc.

counter | todo

counterState

import { RegisterState } from "react-mono-state";

export const counterState: RegisterState<Counter> = {
  stateName: "counter",
  initialState: { loading: false, count: 0 },
  mapActionToState(emit) {
    return {
      inc(state) {
        emit({ loading: false, count: state.count + 1 });
      },
      dec(state) {
        emit({ loading: false, count: state.count - 1 });
      },
      async asyncInc(state) {
        emit({ loading: true, count: state.count });
        await delay(1000);
        emit((c_state) => ({ loading: false, count: c_state.count + 1 }));
      },
    };
  },
};

function delay(ms: number) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

counterComponent

import { useSelector, useDispatch } from "react-mono-state";

export default () => {
  const dispatch = useDispatch();
  const { count, loading } = useSelector((state: AppState) => state.counter);
  return (
    <div>
      <button onClick={(e) => dispatch("inc")}>+</button>
      <button onClick={(e) => dispatch("asyncInc")}>Async(+)</button>
      <button onClick={(e) => dispatch("dec")}>-</button>
      <span>{loading ? "loading..." : count}</span>
    </div>
  );
};

app.ts

import React from "react";
import { Provider, createStore } from "react-mono-state";
import { counterState } from "../states/counterState";

import "../styles/globals.css";

export const store = createStore([counterState]);

function MyApp({ Component, pageProps }) {
  return (
    <Provider store={store}>
      <Component {...pageProps} />
    </Provider>
  );
}

export default MyApp;

Action Handler and Effects search Demo

We are going to develop a search component. This component has a SearchInput child component which dispatches search-input action every times user strokes in the keyboard. The search-input action catches by the useStoreEffect hook which debounces a while so that he can collect some more keys and then send a request to the server to pull the search result and finally dispatches a new action (named search-result) with pulled data.

At the moment search-result action dispatchs - Search component is ready to handle this action by the useActionHandler hook and then finally render the search results.

All these are happening without reducer and middleware. awesome!

Reactive programming is COOL - What a nice combination. its really useful and effective.

SearchInput component

import React from "react";
import { useStoreEffect, useDispatch } from "react-mono-state";
import {
  debounceTime,
  distinctUntilChanged,
  map,
  switchMap,
  filter,
} from "rxjs/operators";
import { from } from "rxjs";

export default () => {
  const dispatch = useDispatch();

  useStoreEffect((action$) =>
    action$.whereType("search-input").pipe(
      debounceTime(320),
      distinctUntilChanged(),
      filter(({ payload }) => !!payload),
      switchMap((action) => getData(action.payload)),
      map((res) => ({ type: "search-result", payload: res }))
    )
  );

  return (
    <div>
      <input
        onChange={(e) =>
          dispatch({ type: "search-input", payload: e.target.value })
        }
        placeholder="search wiki..."
      />
    </div>
  );
};

function getData(text) {
  return from(
    fetch(
      `https://en.wikipedia.org/w/api.php?&origin=*&action=opensearch&search=${text}&limit=5`
    )
      .then((d) => d.json())
      .then((d) => d)
  );
}

Search component

import React from "react";
import SearchInput from "./SearchInput";
import { useActionHandler } from "react-mono-state";
import { map } from "rxjs/operators";

export default () => {
  const [{ loading, data }] = useActionHandler((action$) =>
    action$.whereType("search-result").pipe(map((action) => action.payload))
  );

  const res = loading ? (
    <div>No search result</div>
  ) : (
    data[1].map((text, index) => <div key={index}>{text}</div>)
  );

  return (
    <div>
      <SearchInput />
      {res}
    </div>
  );
};