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

unified-redux-wrapper

v1.3.6

Published

A wrapper around redux , used in conjuction with react to make incorporating state management in your applications easy as a breeze

Downloads

21

Readme

Unified Redux Wrapper

Description

A wrapper built around Redux to simplify the use of Redux which typically involves

  • creating Actions,

  • creating Action Creators,

  • Setting up new Reducers to update slices of the Store,

  • and finally dispatching Actions.

    This cycle needs to be repeated almost every time API requests are made, and essentially anytime the store needs to be updated. This wrapper reduces this verbosity using the same design pattern recommended by the Redux team, it abstracts the repetitive boilerplate involved with setting up new actions, action creators and reducers into a few easy steps.

Problem Statement

The library solves the following problems peculiar to implementing state management using Redux.

  • Verbosity: This wrapper abstracts all the boilerplates and exposes only a few steps.

  • Error propensity: Unified Redux Wrapper limits the chances of errors when writing the multiple artifacts of conventional flux pattern. To update the store, if you have to create actions, action creators, reducers to manage the store for every API call to the back end, you could end up with a lot of unmanaged files where the propensity for introducing error is increased by the number of methods written. This library solves this problem by adopting a very cohesive design pattern where one reducer and one action creator can serve all actions for you.

    Unified Redux Wrapper is modular and highly cohesive, which makes your implementations highly extendable and maintainable. It does not limit you from implementing the conventional approach of Redux you are used to, you can use this approach simultaneously with whatever approach you are familiar with. This is not an extension to Redux, just an implementation of a design pattern that promotes cohesion.

Documentation

  • Typically you will use two methods from this library, which are setUpCombinedReducers and dispatchActions

  • The setUpCombinedReducers combines your reducers (if any ) with those actions you wish to use with the library.

    You need to create a file that contains your actions, conventionally this file is called actionDictionary.js

  • create a variable within this file like this

    const actionDictionary = { }

  • add your actions (as Block cased keys and values separated by an underscore) e.g

 const actionDictionary = {
	MY_FIRST_ACTION: 'MY_FIRST_ACTION',
	MY_SECOND_ACTION: 'MY_SECOND_ACTION'
 }
  • You can update this file when you want to add more items to your store

  • these key/values within the objects become camel-cased in your store so they can be accessed as

    myFirstAction and mySecondAction

  • When creating your store (as defined in the Redux docs) please follow the approach defined below

      import { createStore, applyMiddleware, compose } from "redux";
      import { setUpCombinedReducers } from "unified-redux-wrapper";
      import { actionDictionary } from "./actions/actionDictionary";
      // your defined action dictionary , could be located anywhere
    
      ...
    
      const store = createStore(
      setUpCombinedReducers(actionDictionary),	// here we inject the reducers generated by the library
      initialState,	// you can pass any other base reducer not managed by Unified Redux Wrapper
      composedEnhancers,
      );
    
      export default store;

Using the module in a React component

When creating a component you import the library on the react component as below,

The imported method (dispatchActions) serve as the singular action creator which you can use across the whole application, so it should be passed to mapDispatchToProps as :

import { dispatchActions} from 'unified-redux-wrapper'

Implement mapDispatchToProps as,

const mapDispatchToProps = (dispatch) => {
	return bindActionCreators({
		dispatchActions: dispatchActions,  // other actions not dispatched by the module can be added as well
	}, dispatch);
};

Implement mapStateToProps as,

const mapStateToProps = ({myFirstAction, mySecondAction}) => ({
	myFirstActionPending: myFirstAction.pending 	// here is the pending state, it is false until an asynchronous dispatch is called - this is omited in synchronous action calls.
	myFirstActionPayload: myFirstAction.payload 	// contains payload returned by action.
	myFIrstActionError: myFirstAction.error 	// here is what happens when an error is encountered. Store is updated with the error.
});

Asynchronous updates

Typically each element in your store will have three states:

  1. pending : The pending state will be false initially. It is only present when updating your store with an asynchronous function and the function is being called. The state will be set to true and this is a very good time to add loaders or suspense to your application. When the pending state is true, the payload and the error state will be set to null, the action called during this state will be REQUEST_<Your action>
  2. payload: After the pending state is called and the asynchronous call returns a value, the variable will have a payload state set to the value of the returned object while pending state and error state will be set to null. At this state, the action will be RECIEVE_<Your action>
  3. error: if the asynchronous/synchronous call errors out then the error state will be set to the description of the error encountered, otherwise it is null/false, the action at this state will be FAIL_<Your action>

Synchronous updates

For synchronous updates all the above are true asides from there will be no pending state

Using the Action Creator

The actionCreator requires the following parameters for dispatch

  1. dictKey: This is the value representing the store item you wish to update , referring to our example this will be 'MY_FIRST_STORE'
  2. eventAction: This is a method/values representing the action you want to call or update respectively, typically it could be an API call, for instance, if I have a function that calls my endpoint called callEndPoint. This method will be passed as a variable to the function. For synchronous request, this could be a function that returns values, or the values with which we wish to update the store by.
  3. isAsync: it is true for asynchronous requests and false for synchronous request
  4. parameter: if the event action is a method that requires parameters, please pass an array of this parameter in the order they are called within the method to this variable, if not pass an empty array
  5. actionDictionary: You should pass the action dictionary as the last parameter

To call the dispatchActions within your components

for asynchronous calls

    this.props.dispatchActions(<dictKey>, <eventAction>, true, [<url>, <anyotherparam>], <actionDictionary>)

for synchronous calls

    this.props.dispatchActions(<dictKey>, <eventAction>, false, [<anyparam for functions>], <actionDictionary>)
  • you can pass in functions, promises, or objects as a parameter to the dispatch actions

Example

import { actionDictionary } from '../../actions/actionDictionary';

class Reporting extends Component {

handleTestStore = (inputValue) => {
	const { dispatchActions } = this.props;
	dispatchActions("MY_FIRST_STORE", inputValue, false, [], actionDictionary);
}

render() {
	return (
		<ReportFilters />
		<div className="btn-container">
		<Input type="text" onChange={(e) => this.handleTestStore(e.target.value)} />