oz-redux-reducer
v3.0.1
Published
Builds Redux reducers easily
Downloads
8
Readme
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:
Example:
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"));