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

@djunger02/redux-set-state

v1.0.5

Published

A generic reducer that might be all you need

Downloads

5

Readme

Redux Set State

A generic reducer and action creator for Redux.

build status

yarn add @djunger02/redux-set-state

What is it?

This package provides a generic reducer function and its associated action creator.

Motivation

Redux is great, and I'm a big fan of it, but some folks get very tired of creating distinct actions and their corresponding reducer cases for every interaction in your app. For example, I have seen many projects that have many action creators that look like these:

const TOGGLE_THING = 'TOGGLE_THING'
function toggleThing() {
  return {
    type: TOGGLE_THING
  }
}
const INCREMENT_COUNTER = 'INCREMENT_COUNTER'
function increment() {
  return {
    type: INCREMENT_COUNTER
  }
}
const LOGIN_USER = 'LOGIN_USER'
function login(userMeta) {
  return {
    type: LOGIN_USER,
    payload: userMeta
  }
}

These actions are highly specific and not re-usable, so they quickly accumulate and generate a lot of cruft. However, what they all have in common is that they each want to set a piece of data to a specific part of the Redux store. This module provides a single reducer and action creator that can be used for all of these cases and many more.

Do I Need This?

If you’re not sure whether you need it, you probably don’t. If you're tired of creating unique action creators and reducer cases for every action in your app, give it a try.

Installation

yarn add @djunger02/redux-set-state

Then, add the reducer to your app:

import { createStore } from 'redux';
import { createReducer } from '@djunger02/redux-set-state';

const initialState = {
  todos: {},
  isLoggedIn: false
};
const reducer = createReducer(initialState);
const store = createStore(
  reducer,
  /* applyMiddleware(...) */
);

Then, whenever you have a need to update a part of the store, use the provided action creator:

dispatch(setState('isLoggedIn', true)); // set the top level property 'isLoggedIn' to true
dispatch(setState('user', { email: '[email protected]', name: 'Jane User' })); // set the email and name properties onto the user object
dispatch(setState('models', { '3': {id: 3, ...}, '4': {id: 4, ...} })); // set a number of models into the store
dispatch(setState('models.3', { id: 3, ... })); // overwrite a single model into the store
dispatch(setState('foos.3.bars.4', { id: 3, ... })); // overwrite a single nested model into the store

dispatch(mergeState('user', { name: 'Jane User' })); // merge the name properties onto the user object
dispatch(mergeState('models', { '3': {...}, '4': {...} })); // merge a number of models into the store

Caveats

This module works best if you avoid using arrays in your store data structure because then you would have to care about array indexes while merging data. The reducer uses the excellent library updeep to perform immutable updates to your store object. Updeep handles deeply nested merges, so you never have to worry about whether part of your store path exists or not!

License

MIT