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

lean-redux

v3.3.1

Published

Redux state like local component state

Downloads

28

Readme

Lean Redux Build Status

Redux state like local component state.

Design

  • Basic Redux state access and updating should be simple as it is with the component local state
    • No need to manually create action types or reducers
    • The same API with React Components! Use this.setState() to update Redux state
  • Redux state is be scoped to the components
    • Component cannot interfere with parts of the state that do not belong to it
  • Play well with other tools in the Redux community
    • Time travel debuggers, state serialization tools, Redux Form etc. work well with Lean Redux
    • You can drop this into your existing project and start using it only for parts of the app
  • Good performance
    • Lean Redux is build on top of the new connectAdvanced() primitive of React Redux 5.0 and implements the same optimizations as connect()
    • Handlers are automatically bound to avoid some pure render anti-patterns

Example

import {connectLean} from "lean-redux";

var Counter = ({count, handleClick}) => (
    <div>
        {count} <button onClick={handleClick}>+1</button>
    </div>
);
Counter = connectLean({
    getInitialState() {
        return {count: 0};
    },
    handleClick(e) {
        e.preventDefault();
        this.setState({count: this.state.count + 1});
    },
})(Counter);

// Scope it to a myCounter key in the Redux state
// <Counter scope="myCounter" />

To learn more checkout the live examples.

Setup

npm install --save lean-redux

Just add the leanReducer to your store and start creating components with connectLean.

import {createStore, applyMiddleware} from "redux";
import {Provider} from "react-redux";
import {leanReducer} from "lean-redux";

const store = createStore(leanReducer);

var Main = () => (
    <Provider store={store}>
        <Counter />
    </Provider>
);

ReactDOM.render(<Main />, document.getElementById("app"));

If you already have other reducers you can merge leanReducer into them with the composeReducers helper:

import {leanReducer, composeReducers} from "lean-redux";

const store = createStore(composeReducers(myReducer, myAnotherReducer, leanReducer));

Checkout the index.js in examples for complete example.

Usage with the Redux combineReducers helper

The combineReducers helper function does not like dynamically generated top level state keys so Lean Redux must be scoped under a specific key in the Redux state when used with the combineReducers helper.

import {createStore, combineReducers} from "redux";
import {leanReducer} from "lean-redux";

leanReducer.setGlobalScope("lean");

const store = createStore(combineReducers({
    lean: leanReducer
}));

API

Functions exported by the lean-redux module.

connectLean(options: Object): (component: ReactComponent) => ReactComponent

Connects a React component to a Redux store. Like connect() in React Redux it does not modify the component but returns a new one.

options

  • scope: string|Array|Function Scope the component to a part of the Redux state. Deep scopes can be defined with arrays. Missing path values in the state will be automatically created as objects. If the value is a function it should return the final scope. Parent component props are passed to the function. If scope is passed as a prop from the parent component it will override the value defined here unless it's a function.
  • getInitialState(): Object Create default values for the scoped state. Like React component getInitialState() this is executed only once when the component is mounted.
  • mapState(state: Object, ownProps: Object): Object Modify the state before passing it to the wrapped component. Just like the mapStateToProps in React Redux, but the state is scoped according to the scope option. If not defined the default implementation is to return the props matching what getInitialState() returns.
  • defaultProps: Object Default props for the handler context.

Any other methods are considered to be "handlers" and are passed to the wrapped component as props.

handlerContext

The context, this, in the handlers is like in the React Components.

  • this.state: Object The current scoped state from Redux
  • this.props: Object Props from defaultProps and any additional props passed by the parent component.
  • this.setState(function|object nextState, [function callback]) Function to update the scoped Redux state. The API is exactly the same with the React Component setState().
  • this.dispatch: Function Redux store dispatch