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

react-local-redux

v2.1.1

Published

Manage component-specific state as you would global state via redux

Downloads

17

Readme

react-local-redux

Manage component-specific state as you would global state via redux

Table of contents

Summary

The redux library has changed the way that we manage state within our JavaScript applications, and is a wonderful tool. Like many great tools, though, developers see it as a hammer in a world of nails, using redux for all state regardless of whether it should be global or not. The creator of redux himself has even written about how redux should be used selectively, because redux is meant for global state.

react-local-redux tries to strike a balance, leveraging the powers of the redux paradigm but keeping state that does not need to be global scoped to the component that it should live in by creating a higher-order component that operates like a local store. It's usage should come naturally to those who have used react-redux, you can use redux middlewares and enhancers, and there are even helpers to remove boilerplate related to building action creators and reducers.

Usage

import { connectLocal } from "react-local-redux";

const actionCreators = {
  decrement: () => ({ type: "DECREMENT" }),
  increment: () => ({ type: "INCREMENT" })
};

const reducer = (state = { count: 0 }, action) => {
  switch (action.type) {
    case "DECREMENT":
      return {
        ...state,
        count: state.count - 1
      };

    case "INCREMENT":
      return {
        ...state,
        count: state.count + 1
      };

    default:
      return state;
  }
};

@connectLocal(reducer, actionCreators)
class App extends Component {
  render() {
    const {count, decrement, increment} = this.props;
    
    return (
      <div>
        <div>Count: {count}</div>

        <button onClick={decrement}>Decrement</button>
        <button onClick={increment}>Increment</button>
      </div>
    );
  }
}

connectLocal

connectLocal(reducer: function[, actionCreators: (function|Object)[, options: Object]]) => (ComponentToWrap: ReactComponent): ReactComponent

Decorator that accepts a reducer function and actionCreators, returning a function that accepts a ReactComponent and returns a higher-order ReactComponent.

If actionCreators is an object of functions, it will automatically wrap those functions in dispatch, and if it is a function then it will handle the following contract:

actionCreators(dispatch: function, ownProps: Object): Object

This should operate the exact same as mapDispatchToProps in react-redux.

Internally the reducer will be used as local state, with each of the actionCreators being used to update that state. Both the local state and the wrapped actionCreators will be passed to the ComponentToWrap as props, very much align the lines of connect in the react-redux package. If no actionCreators are passed, the dispatch method itself will be passed as a prop to the ComponentToWrap.

options

Like connect in react-redux, you can pass an object of options to customize how connectLocal will operate:

  • pure: boolean
    • is the component considered a "pure" component, meaning does it only update when props / state / context has changed based on strict equality
  • areOwnPropsEqual(currentProps: Object, nextProps: Object): boolean
    • custom props equality comparator
  • areStatesEqual(currentState: Object, nextState: Object): boolean
    • custom states equality comparator
  • areStatesEqual(mergedProps: Object, nextMergedProps: Object): boolean
    • custom equality comparator for the merged state, actionCreators, and props passed to the component

Additionally, there are some options specific to connectLocal that provide more functionality:

  • enhancer(fn: function): function
    • store enhancer used in redux.createStore()
@connectLocal(reducer, actionCreators, {
  enhancer: window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
    name: 'MySpecialComponent'
  })
})
  • middlewares: Array<function>
    • array of redux middlewares to be appled to the dispatch method
@connectLocal(reducer, actionCreators, {middlewares: [reduxThunk]})
  • mergeProps(state: Object, actionCreators: Object, props: Object): Object
    • custom method to merge state, actionCreators, and props into the props passed to the component

Additional imports

Additional imports are available for handling construction of action creators and reducers. The paradigm will be familiar to those who have used the redux-actions library.

createActionCreator

createActionCreator(type: string[, payloadHandler: function = identity[, metaHandler: function = noop]]): function

Create a Flux Standard Action in the same vein as redux-actions.

const addStuff = createActionCreator("ADD_STUFF");

addStuff({ added: "stuff" });
// {payload: {added: 'stuff'}, type: 'ADD_STUFF'}

By default, the first parameter passed to the action creator will be used as the payload, but you can provide custom handlers for the payload and the meta if desired.

const addStuff = createActionCreator(
  "ADD_STUFF",
  ({ added }) => added,
  ({ added }) => added === "stuff"
);

addStuff({ added: "stuff" });
// {meta: true, payload: 'stuff', type: 'ADD_STUFF'}

Additionally, if an error is provided as the payload, an error property will be automatically set to true on the action.

createReducer

createReducer(handlers: Object, initialState: Object): function

Create a reducer based on a map of handlers, each themselves a reducer.

const handlers = {
  SET_THING; (state, {payload}) => ({
    ...state,
    thing: payload,
  }),
};

const initialState = {
  thing: null,
};

const reducer = createReducer(handlers, initialState);

const result = reducer(initialState, {payload: 'new thing', type: 'SET_THING'});
// {thing: 'new thing'}

Development

Standard stuff, clone the repo and npm install dependencies. The npm scripts available:

  • build => run webpack to build development dist file with NODE_ENV=development
  • build:minified => run webpack to build production dist file with NODE_ENV=production
  • dev => run webpack dev server to run example app / playground
  • dist => runs build and build:minified
  • lint => run ESLint against all files in the src folder
  • prepublish => runs prepublish:compile when publishing
  • prepublish:compile => run lint, test:coverage, transpile:es, transpile:lib, dist
  • test => run AVA test functions with NODE_ENV=test
  • test:coverage => run test but with nyc for coverage checker
  • test:watch => run test, but with persistent watcher
  • transpile:lib => run babel against all files in src to create files in lib
  • transpile:es => run babel against all files in src to create files in es, preserving ES2015 modules (for pkg.module)