Skip to content

ofir-zeitoun/oz-redux-reducer

Repository files navigation

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 😉

About

A lightweight wrapper for Redux, simpler then RTK

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published