oz-redux-reducer
TypeScript icon, indicating that this package has built-in type declarations

3.0.1 • Public • Published

oz-redux-reducer

This utility reduces the boilerplate for Redux store reducers.

In one function you will have both the reducer and the actions without the need for types.

No more switch cases

No more types

Works with:

  1. Redux
  2. Redux Thunk
  3. React Redux

Example:

Live demo

Install:

npm i oz-redux-reducer

Usage:

./src/store/reducers/demo-reducer.ts

import { Dispatch } from "redux";
import { buildReducer } from "oz-redux-reducer";

export const [demoReducer, demoActions] = buildReducer({
  // initial state
  text: "<some init value>s",

  // actions
  setText(state: object, newValue: string) {
    return { ...state, text: newValue };
  },

  async fetchText(dispatch: Dispatch) {
    const value = await fetch(/*...*/);
    dispatch(demoActions.setText(value));
  };
});

.src/store/actions.ts

export { demoActions } from "./reducers/demo-reducer.ts

./src/store/reducers/index.ts

import { combineReducers } from "redux";
import { demoReducer } from "./demo-reducer";

export default combineReducers({
  demo: demoReducer,
});

calling actions:

import { demoActions } from "./demo-reducer";

// in compnent or hook
dispatch(demoActions.setText("newText"));

Now replace old reducers with new ozReducer 😉

Readme

Keywords

Package Sidebar

Install

npm i oz-redux-reducer

Weekly Downloads

0

Version

3.0.1

License

MIT

Unpacked Size

29.9 kB

Total Files

25

Last publish

Collaborators

  • ofir-zeitoun