combine-reducers-react
v1.0.3
Published
A tiny package for combine reducers that mimic the actual combine reducers of redux while using useReducers with contextApi
Downloads
20
Maintainers
Readme
A less than 10kb packages is mimics the logic behind the combineReducers of redux. It can be used with React hook useReducer
Installation
npm i combine-reducers-react
Basic usage
const [state, reducer] = combineReducers({
first: [firstInitalState, firstReducer],
second: [secondInitialState, secondReducer]
})
`
Working example
import combineReducers from 'combine-reducers-react'
const userInitialState = {
currentUser: {},
isloggedIn: false,
basicDetails: {
name: null,
email: null
}
}
const cartInitialState = {
itemsAdded: [],
paymentSummary: {}
}
const userReducer = (state, action) => {
const {type, ...data} = action
switch(type){
case 'TOGGLE_LOGIN': {
return {...state, isloggedIn: data.status}
}
default: {
return state
}
}
}
const cartDetailsReducer = (state, action) => {
const {type, ...data} = action
switch(type){
case 'ADD_ITEMS': {
return {...state, itemsAdded: [...state.itemsAdded, data.item ]}
}
default: {
return state
}
}
}
const [state, reducer] = combineReducers({
user: [userInitialState, userReducer],
cartDetails: [cartInitialState, cartDetailsReducer]
})
console.log(reducer(state, {type: 'ADD_ITEMS', item: {item: 'ball', quantity: 3}}))
console.log(reducer(state, {type: 'TOGGLE_LOGIN', status: true}))
Issues Raise a issue
Please do leave a star if it helps you