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

rezux

v0.0.1

Published

React state management. One API rule them all

Downloads

2

Readme

Rezux

React state management. One API rule them all.

Counter App

import useStore from "rezux";

// define action types
const INCREASE = "increase";
const DECREASE = "decrease";

// define store, store is just pure function, it works like Redux reducer
const initialState = 1;
const Store = (state = initialState, action, payload = 1) => {
  if (action === INCREASE) return state + payload;
  if (action === DECREASE) return state - payload;
  return state;
};

// display store state
// this component will be re-rendered when store state changed
const CounterValue = () => {
  const count = useStore(Store, (store) => store.state());
  return <h1>{count}</h1>;
};

// dispatch store actions
// this component renders once
const CounterActions = () => {
  // retrieve dispatch method from store
  const { dispatch } = useStore(Store);
  return (
    <>
      <button onClick={() => dispatch(INCREASE)}>Increase</button>
      <button onClick={() => dispatch(INCREASE, 2)}>Increase by 2</button>
      <button onClick={() => dispatch(DECREASE)}>Decrease</button>
    </>
  );
};

Handling state changing and action dispatching

import { useEffect } from "react";
import useStore from "rezux";

const Store = () => {};
const handleAnyActionDispatching = () => {};
const handleStateChanging = ({ store }) => {
  localStorage.setItem("appData", JSON.stringify(store.state()));
};
const handleIncreaseActionDispatching = () => {
  console.log("increase");
};

const App = () => {
  const store = useStore(Store);

  useEffect(() => {
    store.subscribe(handleStateChanging);
    store.subscribe(handleAnyActionDispatching, "*");
    store.subscribe(handleIncreaseActionDispatching, "increase");
  }, []);
};

Advanced Usages

Async dispatching

import useStore from "rezux";
const INCREASE = "increase";
const DECREASE = "decrease";
const INCREASE_ASYNC = async (by, store) => {
  await delay(1000);
  store.dispatch(INCREASE, by);
};
// define store, store is just pure function, it works like Redux reducer
const initialState = 1;
const Store = (state = initialState, action, payload = 1) => {
  if (action === INCREASE) return state + payload;
  if (action === DECREASE) return state - payload;
  return state;
};

const CounterActions = () => {
  // retrieve dispatch method from store
  const { dispatch } = useStore(Store);
  return (
    <>
      <button onClick={() => dispatch(INCREASE_ASYNC)}>Increase Async</button>
    </>
  );
};

Listening future action dispatching

const SEARCH_PRODUCT = "search_product";
const SEARCH_PRODUCT_SUCCESS = "search_product_success";
const UPDATE_PRODUCT_LIST = "update_product_list";
const CANCEL = "cancel";
const SEARCH_PRODUCT_EPIC = async (_, { dispatch, any }) => {
  // run forever
  while (true) {
    const [keyword] = await any(SEARCH_PRODUCT);
    dispatch(SEARCH_PRODUCT_API, keyword);
    // listen SEARCH_PRODUCT_SUCCESS or CANCEL
    const [payload, action] = await any(SEARCH_PRODUCT_SUCCESS, CANCEL);
    // user cancels search progress before SEARCH_PRODUCT_SUCCESS dispatched
    if (action === CANCEL) {
      // do nothing and continue listening
      continue;
    }
    // update product list when SEARCH_PRODUCT_SUCCESS dispatched
    dispatch(UPDATE_PRODUCT_LIST, payload);
  }
};

const SEARCH_PRODUCT_API = async (keyword, { dispatch }) => {
  const response = await axios("api-url");
  dispatch(SEARCH_PRODUCT_SUCCESS, response.data);
};

Action dispatcher

const { increase, decrease } = useStore(Store, (store) => ({
  // store is function, so if you pass action type to store, it returns memoized action dispatcher
  increase: store(INCREASE),
  decrease: store(DECREASE),
}));

increase(100);
decrease(99);

Using reducer map as store declaration

const INCREASE = "increase";
const DECREASE = "decrease";
const Store = {
  initial: 1,
  [INCREASE]: (state, by = 1) => state + by,
  [DECREASE]: (state, by = 1) => state - by,
};