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

simple-redux-collection

v1.0.3

Published

Helper for creating and managing redux collections

Downloads

5

Readme

simple-redux-collection - Redux helper for simple collection management

Codecov Coverage

Motivation

We work with collections every day, and creating monotonous reducers and actions can be quite tedious. So I decided to create this helper to minimize code duplication.

Demo

Include redux actions for:

  • set a new item
  • set new items
  • delete item
  • reset collection to initial state

Install

You can install simple-redux-collection with npm or yarn

$ npm i simple-redux-collection -s 

or

yarn add simple-redux-collection

Documentation

import collectionReducer from "simple-redux-collection";

collectionReducer({name, key, initialState})

options

name: String [required] - reducer name, all actions types will be created in @{name}/action_type format

initialState : Object [optional] - reducer inital state (empty object by default)

Example of reducer state:

{
    uid1: {uid: 'uid1', title: 'title'},
    uid2: {uid: 'uid2', title: 'title 2'},
}
Return object {ACTIONS_TYPES, actionCreators, reducer}

ACTIONS_TYPES: Object - all action type constants, can be used in middleware

SET_ITEM: @{name}/set_item,
SET_ITEMS: @{name}/set_items,
REMOVE_ITEM: @{name}/remove_item,
RESET: @${name}/reset,

actionCreators: Object - object of actions creators:

setItem: ({key, value})

setItems: ([{key, value}]), 

removeItem: (id: String|Number)

reset: ()

reducer: (state: Object, action: Object) => state - regular Redux reducer function

Example

Let's create collection for list of comments

Reducer file:

import createReducer from "simple-redux-collection";

const initialState = {
  id1: { id: "id1", title: "Commemt title", time: "30/11/2019, 22:21:29" }
};
const { reducer, actionCreators } = createReducer({name: "comments", initialState});

// reducer function should be added to a root reducer
export { reducer, actionCreators };

Container file:

import { connect } from "react-redux";
import { actionCreators } from "./reducer";
import Comments from "./Comments";

const { setItem, setItems, removeItem, reset } = actionCreators;

const mapStateToProps = state => ({
  comments: state.comments
});

const mapDispatchToProps = {
  setItem, setItems, removeItem, reset
};

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(Comments);

Now all actions and collection items can be used as props in the Comments component.

import PropTypes from "prop-types";
import React, { useState } from "react";

const Comments = ({ comments, setItem, removeItem, reset }) => {
  const [inputValue, changeCommentText] = useState("");

  const addNewComment = () => {
    // id is required for creating new item
    const id = String(Math.random());
    setItem({
      key: id,
      value: {
        id,
        title: inputValue,
        time: new Date().toLocaleString()
      }
    });

    changeCommentText("");
  };

  return (
    <div>
      <input
        onChange={e => changeCommentText(e.target.value)}
        value={inputValue}
      />
      <button onClick={addNewComment}>Post new comment</button>
      <div>
        {/* you can use your selector in container to return data in required format */}
        {Object.values(comments).map(({ id, title, time }) => (
          <div key={id} className="item">
            <b>{title}</b> <i>Comment time: {time}</i>
            <button
              onClick={() =>
                // id use as the `key` in reducer and it's required for updating item
                setItem({
                  key: id,
                  value: { id, title, time: new Date().toLocaleString() }
                })
              }
            >
              Update comment time
            </button>
            <button className="delete" onClick={() => removeItem(id)}>
              Delete
            </button>
          </div>
        ))}
      </div>
      <button className="reset" onClick={reset}>
        Reset
      </button>
    </div>
  );
};

Comments.propTypes = {
  comments: PropTypes.array,
  setItem: PropTypes.func.isRequired,
  removeItem: PropTypes.func.isRequired,
  reset: PropTypes.func.isRequired,
};

export default Comments;

Check the full code example