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:
npm i oz-redux-reducer
./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"));