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

redux-updaters

v1.1.0

Published

Update your Redux state directly from your components. No reducers, no actions.

Downloads

33

Readme

npm Build Status

Update your state directly from your components. No reducers, no actions.

Built upon Redux to keep all of its benefits, this package brings centralised state management closer to the component architecture.

Resulting benefits are:

:white_check_mark: Greatly reduce lines of code in your projects

:white_check_mark: Reduce code complexity

:white_check_mark: Increase maintainability

:white_check_mark: Reduce chance of bugs

Also:

:white_check_mark: Respects the priciples of Redux, keeping all of its benefits

:white_check_mark: Can be added to existing code bases that already use Redux

Installation

npm i redux-updaters redux-thunk

or

yarn add redux-updaters redux-thunk

Note: this package requires redux-thunk middleware. You need to add this yourself, see example below

The gist

Read the article on Medium

This package is designed to provide an interface to using Redux that better matches the component architecture. Redux prescribes separating state management logic from your component logic, through actions and reducers. We believe that when using the component architecture, components should be leading. In practice, developers don't reason from a state management perspective, but rather from the perspective of your component implementations. State management happens whenever your components need it. At this point you would create actions and reducers, because your components have a need for it. Using Redux Updaters, you can just update your state directly from your components when you need to. It lets Redux take care of updating the state, keeping all the benefits around keeping state in sync. Using Redux Updaters, you will need only two steps:

  • Define your initial state.
  • Update the state directly from your components.

How does it work

Redux Updaters provides a toolbox of action creators, called updaters, and a reducer to handle the created actions. You are in charge of dispatching the actions as usual. Using the updaters, you should be able to easily update your state in which ever way you need to. Reading state is done the same way as without Redux Updaters.

API

For an overview of all available updater functions, refer to the full API documentation

Quick usage

Define your default state

The default state is like the default state of any reducer, except now it is for the entire state that you wish to control using redux-updaters. This means you can define your entire state tree in here.

// The default state us just a plain object
export default {
    pager: {
        currentIndex: 0
    }
}

Bind the reducer to your redux store

You can add the reducer as the root reducer when creating your store, but we recommend using combine reducers so you have the option to add other redux related packages, for example to manage your api calls and data.

import { createReducer } from 'redux-updaters';
import { combineReducers, createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import defaultState from './defaultState';

const reducer = createReducer(defaultState);
const rootReducer = combineReducers({app: reducer});
const store = createStore(rootReducer, applyMiddleware(thunk));

Use the state in your components - React example

This package has been tested in combination with React, so we use React in our examples, but it does not depend on React and can also be used in combination with other view-libraries or frameworks.

import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { update } from 'redux-updaters';

const MyComponent = props => {
    // Use react-redux to get your state values and dispatch updates.
    const currentIndex = useSelector(state => state.app.pager.currentIndex);
    const dispatch = useDispatch();

    return (
        <div>
            <div>The current index is {currentIndex}</div>
            <div>
                {[0, 1, 2, 3].map(index =>
                    <button key={index} onClick={() => dispatch(update('pager.currentIndex', index))}>{index}</button>,
                )}
            </div>
        </div>
    );
};

export default MyComponent;

Using with TypeScript

This package includes TypeScript type definitions. So type checking on all functions should be enabled by default. To add type checking on the path names given to updater functions, you can use the createStatePaths function to retrieve a StatePathTree. This is a recursive structure that you can use instead of strings when passing the state path to an updater function.

import { createStatePaths } from 'redux-updaters';

export const paths = createStatePaths(defaultState);

// somewhere in your component
dispatch(update(paths.pager.currentIndex, 3));
// is same as
dispatch(update('pager.currentIndex', 3)); 
// but with type checking. Catch errors before they happen!

License

MIT