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

@colincamp/react-redux-functions

v1.0.3

Published

utilities for using redux with react hooks

Downloads

3

Readme

react-redux-functions

React Redux Functions is a utility package for making Redux easier to use with hooks and reduce (pun intended) the boilerplate for defining new actions and reducers.

There are some built in utility functions for common tasks such as useGetValue for reading values in redux and setValue for setting a value in redux

Example Usage

import { Provider, useReduxFunctions } from "@colincamp/react-redux-functions";

const initialState = {
  users: [{ id: 1, model: "users", name: "Colin", permission: "admin", upvotes: 0 }],
};

function Root() {
  return (
    <Provider config={{ initialState }}>
      <FirstUser path={["users", 0]} />
      <UpvoteFirstUser path={["users", 0]} />
    </Provider>
  );
}

function FirstUser({ path }) {
  const { useGetValue } = useReduxFunctions();
  const { name, permission, upvotes } = useGetValue(path, {});
  return (
    <p>{`the first user ${name} with permission ${permission} has been upvoted ${upvotes} times`}</p>
  );
}

function UpvoteFirstUser({ path }) {
  const { useGetValue, setValue } = useReduxFunctions();
  const { name, upvotes } = useGetValue(path, {});
  const handleUpvote = () => {
    setValue({
      path: [...path, "upvotes"],
      value: upvotes + 1,
    });
  };
  return <button onClick={handleUpvote}>{`Click to upvote ${name}`}</button>;
}

const root = createRoot(document.getElementById("container"));
root.render(<Root />);

Extending with Custom Actions and Reducers

When calling the Provider component you can provide an actions object in the config prop. This object is a mapping between and action name and a reducer function in the simple case

const actions = {
  [actionName]: reducerFn,
};

In this case the action name is also the type in the payload once the action is dispatched.

You can also use the object definition if you would like to provide a custom type for the action.

const actions = {
  [actionName]: {
    reducer: reducerFn,
    type: "CUSTOM_ACTION_TYPE",
  },
};

The actions defined will then be available to pull out of the useReduxFunctions hook, and when called will dispatch that action with the payload provided as first argument in the function call. The optional second argument will be the meta in the action object.

// Inside some component:
const { actionName } = useReduxFunctions();
const payload = { some: { payload: [] } };
const meta = { component: "SomeSneakyComponent" };
actionName(payload, meta);
/*
  action:
  {
    type: actionName || "CUSTOM_ACTION_TYPE",
    payload: { some: { payload: [] } },
    meta: { component: "SomeSneakyComponent" },
  }
*/

Custom Action Example

import { Provider, useReduxFunctions } from "@colincamp/react-redux-functions";
import * as R from "ramda";

const initialState = {
  users: [{ id: 1, model: "users", name: "Colin", permission: "admin", upvotes: 0 }],
};

const addToValueReducer = (state, action) => {
  const payloadPath = R.path(["payload", "path"], action);
  const payloadValue = R.pathOr(1, ["payload", "value"], action);
  return R.pipe(
    R.pathOr(0, payloadPath),
    R.add(payloadValue),
    R.assocPath(payloadPath, R.__, state)
  )(state);
};

const actions = {
  addToValue: {
    reducer: addToValueReducer,
    type: "ADD_TO_VALUE",
  },
};

function Root() {
  return (
    <Provider config={{ initialState, actions }}>
      <FirstUser path={["users", 0]} />
      <UpvoteFirstUser path={["users", 0]} />
    </Provider>
  );
}

function FirstUser({ path }) {
  const { useGetValue } = useReduxFunctions();
  const { name, permission, upvotes } = useGetValue(path, {});
  return (
    <p>{`the first user ${name} with permission ${permission} has been upvoted ${upvotes} times`}</p>
  );
}

function UpvoteFirstUser({ path }) {
  const { useGetValue, addToValue } = useReduxFunctions();
  const { name, upvotes } = useGetValue(path, {});
  const handleUpvote = value => () => {
    /*
    action dispatched:
    {
      type: "ADD_TO_VALUE",
      path: ["users", 0, "upvotes],
      value: value (undefined or 5)
    }
    */
    addToValue({
      path: [...path, "upvotes"],
      value,
    });
  };
  return (
    <>
      <button onClick={handleUpvote()}>{`Click to upvote ${name} one time`}</button>
      <button onClick={handleUpvote(5)}>{`Click to upvote ${name} five times`}</button>
    </>
  );
}

const root = createRoot(document.getElementById("container"));
root.render(<Root />);

TODO

  • ~Make config dynamically generate reducers and actions~
  • Switch to the builder callback notation (see https://redux-toolkit.js.org/api/createReducer)
  • ~Support action definition with object or function (use the key for the action type in this case)~
  • Support defining selectors and binding to useSelector -- Also support selector creators?
  • Handle reducer slices
  • Documentation