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-simple-redux

v0.2.2

Published

A minimal implementation of react-redux using React Context API

Downloads

20

Readme

React Simple Redux

A minimal implementation of react-redux using React Context API. The API completely follows how react-redux works.

To migrate your project from react-redux to react-simple-redux or the other way around, You only need to change the configuration of Provider. Rest of your code will still work.

Why

  • You are building a component in a big project that uses react-redux and you don't want to pollute the global store before your component is really ready.
  • Your project is too small to use full-blown redux, but it's complex enough to have state management in it.
  • You need to publish a React component, but you cannot avoid props-drilling in your component.
  • You don't want to put the Provider at the top level for some reason.

Usage

npm i react-simple-redux
// or
yarn add react-simple-redux

First, create a separate file store.js to instantiate the components. And then plug these components into your app.

import createStore from "react-simple-redux";
import reducer from "./path/to/reducer";

const { Provider, Consumer, connect } = createStore(reducer);

export { Provider, Consumer, connect };

Wrap your component with Provider. It doesn't have to be the direct parent, nor at the top level of your app.

Let's say you want to connect UserProfile to store, insert Provider like this.

import { Provider } from './store';

const initialState = { firstName: 'Jon', lastName: 'Snow', email: '[email protected]' }

<Dashboard>
    <Provider initialState={initialState} >
        <Header>
            <UserProfile />
        </Header>
    </Provider>
</Dashboard>
// reducer.js

const SET_USERNAME = 'action/set_username';

export default function reducer(state, action){
    const { type, payload } = action;

    // To log all the actions
    // console.log(type, payload);

    switch(type){
        case SET_USERNAME: {
            const { firstName, lastName } = payload;
            return {...state, firstName, lastName};
        }
        default: {
            return state;
        }
    }
}

// Action creator
export const setUsernameAction = ({ firstName, lastName }) => ({
    type: SET_USERNAME,
    payload: { firstName, lastName }
});

Finally, define how data and dispatchers should work in your child component. Note: you need to wrap your component with React.memo. It is necessary to prevent unnecessary re-render caused by state updates.

// UserProfile.jsx

import { connect } from './path/to/store';
import { setUsernameAction } from './path/to/your/reducer';

const UserProfile = ({ username, email, setUsername }) => {
    return (
        <div>
            <div>
                <div>User Name: {username}</div>
                <div>Email: {email}</div>
            </div>
            <button onClick={() => setUsername({firstName: 'Kit', lastName: 'Harington'})}>
                Rename to Kit Harington
            </button>
        </div>
    )
}

const mapStateToProps = state => {
    const { firstName, lastName } = state;
    return { username: firstName + ' ' + lastName }
}

const mapDispatchToProps = dispatch => ({
    setUsername: newName => dispatch(setUsernameAction(newName)),
})

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(React.memo(UserProfile)); // use React.memo here

For more extensive documentation, plase check react-redux.

If there is any inconsistent usage with react-redux, please file an issue.