immer-reducer
Advanced tools
Comparing version 0.7.5 to 0.7.6
@@ -69,2 +69,11 @@ import { Draft } from "immer"; | ||
}, immerReducerClass: T): action is Actions<T>; | ||
interface Reducer<State> { | ||
(state: State | undefined, action: any): State; | ||
} | ||
/** | ||
* Combine multiple reducers into a single one | ||
* | ||
* @param reducers two or more reducer | ||
*/ | ||
export declare function composeReducers<State>(...reducers: (Reducer<State | undefined>)[]): Reducer<State>; | ||
/** The actual ImmerReducer class */ | ||
@@ -71,0 +80,0 @@ export declare class ImmerReducer<T> { |
@@ -38,2 +38,22 @@ "use strict"; | ||
exports.isActionFrom = isActionFrom; | ||
/** | ||
* Combine multiple reducers into a single one | ||
* | ||
* @param reducers two or more reducer | ||
*/ | ||
function composeReducers() { | ||
var reducers = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
reducers[_i] = arguments[_i]; | ||
} | ||
return function (state, action) { | ||
return (reducers.reduce(function (state, subReducer) { | ||
if (typeof subReducer === "function") { | ||
return subReducer(state, action); | ||
} | ||
return state; | ||
}, state) || state); | ||
}; | ||
} | ||
exports.composeReducers = composeReducers; | ||
/** The actual ImmerReducer class */ | ||
@@ -40,0 +60,0 @@ var ImmerReducer = /** @class */ (function () { |
{ | ||
"name": "immer-reducer", | ||
"version": "0.7.5", | ||
"version": "0.7.6", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/immer-reducer.js", |
@@ -252,2 +252,31 @@ # immer-reducer | ||
**Warning:** Due to how immer-reducers action generation works, adding default | ||
parameters to the methods will NOT pass it to the action payload, which can | ||
make your reducer impure and the values will not be available in middlewares. | ||
```ts | ||
class MyImmerReducer extends ImmerReducer<State> { | ||
addItem (id: string = uuid()) { | ||
this.draftState.ids.push([id]) | ||
} | ||
} | ||
immerActions.addItem() // generates empty payload { payload: [] } | ||
``` | ||
As a workaround, create custom action creator wrappers that pass the default parameters instead. | ||
```ts | ||
class MyImmerReducer extends ImmerReducer<State> { | ||
addItem (id) { | ||
this.draftState.ids.push([id]) | ||
} | ||
} | ||
const actions = { | ||
addItem: () => immerActions.addItem(id) | ||
} | ||
``` | ||
## 📚 Examples | ||
@@ -327,1 +356,32 @@ | ||
``` | ||
### `function composeReducers<State>(...reducers)` | ||
Utility that reduces actions by applying them through multiple reducers. | ||
This helps in allowing you to split up your reducer logic to multiple `ImmerReducer`s | ||
if they affect the same part of your state | ||
Example | ||
```ts | ||
class MyNameReducer extends ImmerReducer<NamesState> { | ||
setFirstName(firstName: string) { | ||
this.draftState.firstName = firstName; | ||
} | ||
setLastName(lastName: string) { | ||
this.draftState.lastName = lastName; | ||
} | ||
} | ||
class MyAgeReducer extends ImmerReducer<AgeState> { | ||
setAge(age: number) { | ||
this.draftState.age = 8; | ||
} | ||
} | ||
export const reducer = composeReducers( | ||
createReducerFunction(MyNameReducer, initialState), | ||
createReducerFunction(MyAgeReducer, initialState) | ||
) | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27580
295
386