Socket
Socket
Sign inDemoInstall

reshow-flux-base

Package Overview
Dependencies
Maintainers
1
Versions
132
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reshow-flux-base

Simplify state management with no dependencies, yet powerful.


Version published
Weekly downloads
395
decreased by-30.7%
Maintainers
1
Weekly downloads
 
Created
Source

createReducer

Similar with react useReducer, but let you use anywhere.

  • GIT
    • https://github.com/react-atomic/reshow/tree/main/packages/reshow-flux-base
  • NPM

Usage

Accepts a reducer function of type (state, action) => newState. Returns a new store with a dispatch method.

import { createReducer } from "reshow-flux-base";

/**
 * reducer -> (state, action) => newState
 */
const [store, dispatch] = createReducer(reducer, initial[Arg | Function]);

Store methods.

MethodsExplain
getStateReturn current state.
addListenerYou could register any callback function such as react useState.
removeListenerRemove register callback, such as unmount a component.

Full App Example

const initialState = { count: 0 };

function reducer(state, action) {
    switch (action.type) {
        case "increment":
            return { count: state.count + 1 };
        case "decrement":
            return { count: state.count - 1 };
        default:
            throw new Error();
    }
}

const [store, dispatch] = createReducer(reducer, initialState);

function Counter() {
    const [state, setState] = useState(() => store.getState());
    useEffect(() => {
        store.addListener(setState);
        return () => {
            store.removeListener(setState);
        };
    }, []);

    return (
        <>
            Count: {state.count}
            <button onClick={() => dispatch({ type: "decrement" })}>-</button>
            <button onClick={() => dispatch({ type: "increment" })}>+</button>
        </>
    );
}

Codesandbox

https://codesandbox.io/s/reshow-flux-base-34umk

Keywords

FAQs

Package last updated on 25 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc