@sixgramwater/redux-lite
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

redux-lite

simple library for state management inspired by redux and react-redux

NPM JavaScript Style Guide

Install

npm install --save @sixgramwater/redux-lite

Usage

// store/counter.ts
import { fetch } from "../api";

const counterState = {
  count: 0,
}

// A single module named: counter
const counter = {
  state: counterState,
  reducers: {
    addCounter({ state, payload }) {
      return {
        ...state,
        count: state.count + payload,
      }
    }
  },
  effects: {
    asyncAddCounter({dispatch, state, payload}) {
      fetch(1000).then(value => {
        dispatch.counter.addCounter(payload);
      })  
    }
  }
}

// setup modules and invoke create()
const modules = { counter };
export const { useModule, dispatch, useSelector } = create(modules);


// App.jsx
import { useModule, dispatch } from './store';

const App = () => {
  // const counterState = useModule('counter');
  // const count = counterState.count;
  const count = useSelector('counter', state => state.count);
  const handleClick = () => {
    dispatch.counter.addCounter(1);
  }

  const handleAsyncClick = () => {
    dispatch.counter.asyncAddCounter(1);
  }
  return (
    <div>
      {count}
      <button onClick={handleClick}>increase</button>
      <button onClick={handleAsyncClick}>Async increase</button>
      
    </div>
  )
}

License

MIT © sixgramwater


This hook is created using create-react-hook.

Dependencies (0)

    Dev Dependencies (19)

    Package Sidebar

    Install

    npm i @sixgramwater/redux-lite

    Weekly Downloads

    13

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    675 kB

    Total Files

    34

    Last publish

    Collaborators

    • sixgramwater