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

redux-copier

v0.0.4

Published

redux-copier makes redux module support multi-instance.

Downloads

1

Readme

redux-copier

redux-copier helps to make High Order Component reusable again.

why need redux-copier

what is redux module

React Component is reusable. We make a component and place it on many different pages for many times.

Component is the View in MVC. Redux take charge of the MC part. Someday an idea occurs, why do we pack action and reducer together as a reusable module? We call it redux module or a duck.

what is HOC(High Order Component)

Read this from react official site. It's a brilliant idea and that's exactly how connect in react-redux works.

The process of creating HOC is more like jigsaw game.

component require property(data or function)

component

duck provide data and function

duck

connect

connect

HOC essentially is still a component. That means we can connect with more other ducks.

connect more

connect-more

something wrong?

Assume SimpleComponent is pure React Component, HocComponent is a component produced by react-redux connect.

React Component is reusable.

<div>
  <SimpleComponent />
  <SimpleComponent />
</div>

two instances from one abstract component function. that's all right.

But when

<div>
  <HocComponent />
  <HocComponent />
</div>

something wrong happens. Because two HocComponents share the same data in redux store tree. They have the same behavior! HOC Component is not reusable here.

That's why we need redux-copier, making HOC Component reusable again.

API

copifyActions

function copifyActions(actionCreators, customKey) {
  // ....
  // ....
  return {
    actionCreators: newActionCreators,
    selector: dataSelector
  }
}

param:

  • actionCreators: object, key is just a name, value must be an actionCreator.
  • customKey: string. if customKey is undefined, function will generate a random key inside. see more in section how it works.

return:

  • newActionCreators: object, same shape with param actionCreators.
  • dataSelector: function. see more in section how it works.

copifyReducer

function copifyReducer(reducer) {
  // ...
  // ...
  return newReducer
}

param:

  • reducer: reducer shape function.

return:

  • newReducer: reducer shape function too.

test

npm install
npm run test

Highly recommend reading test script to see how to use redux-copier API

example

There are two examples copied from offical redux repo. A little modifications ensure them to work with redux-copier. Highly recommend you run example and check code.

git clone https://github.com/DreamAndDead/redux-copier
cd examples
cd counter (or todos)
npm i
npm start

how it works

A duck is made up with two part: data and interface to mutate data. Data is placed in redux store; interface is action, we dispatch action causing reducer to mutate data in store.

Let's talk about an example.

Assume addTodo is an action creator, reducer handle the action and add a new todo item in store data, { todos: [..., newItem] }

If we connect the duck with a component, component should retrive todos array as its data source and be able to trigger addTodo. Evething's fine except it'll make hoc doesn't work.

Here's what redux-copier does.

To action, every action is identified with a key. Same action creators connected with different hoc produce actions with different key.

To reducer, reducer analyze actions it receives and update different data field marked by action key.

To data, every key marks a field of data. { todos: [...] } is more like

{
  todos: {
    k1: [...],
    k2: [...],
    ...
  }
}

If you know my key, you know every thing, customKey in API section is the key we describe.

selector select data needed by key from todos object.

LISENCE

MIT