Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

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

Pure flux dispatch mechanism

  • 0.17.31
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
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 of type (state, action) => newState, and returns the current store with a dispatch method.

import { createReducer } from "reshow-flux-base";
const [store, dispatch] = createReducer(reducer, initial[Arg|Function]);

/**
 * reducer -> (state, action) => newState
 */

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 16 Apr 2023

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