@@ -77,6 +77,22 @@ const reducer = reducerWithInitialState(INITIAL_STATE)
7777Everything is typesafe. If the types of the action payload and handler don't line up, then
7878TypeScript will complain.
7979
80+ If the full action is needed rather than just the payload, ` .caseWithAction() ` may be used in
81+ place of ` .case() ` . For example:
82+ ``` javascript
83+ import { Action } from " typescript-fsa" ;
84+
85+ const setText = actionCreator< string> (" SET_TEXT" );
86+
87+ const reducer = reducerWithInitialState ({ text, lastEditBy: " " })
88+ .caseWithAction (incrementCount, (state , { payload, meta }) => ({
89+ text: payload,
90+ lastEditBy: meta .author ,
91+ }));
92+
93+ // Returns { text: "hello", lastEditBy: "cbrontë" }.
94+ reducer (undefined , setText (" hello" , { author: " cbrontë" }));
95+ ```
8096The reducer builder chains are mutable. Each call to ` .case() ` modifies the callee to respond to the
8197specified action type. If this is undesirable, see the [ ` .build() ` ] ( #build ) method below.
8298
@@ -169,11 +185,17 @@ function reducer(state: State, action: Redux.Action): State {
169185
170186### Reducer chain methods
171187
172- #### ` .case(actionCreator, handler) `
188+ #### ` .case(actionCreator, handler(state, payload) => newState ) `
173189
174190Mutates the reducer such that it applies ` handler ` when passed actions matching the type of
175191` actionCreator ` . For examples, see [ Usage] ( #usage ) .
176192
193+ #### ` .caseWithAction(actionCreator, handler(state, action) => newState) `
194+
195+ Like ` .case() ` , except that ` handler ` receives the entire action as its second argument rather
196+ than just the payload. This is useful if you want to read other properties of the action, such as
197+ ` meta ` or ` error ` . For an example, see [ Usage] ( #usage ) .
198+
177199#### ` .build() `
178200
179201Returns a plain reducer function whose behavior matches the current state of the reducer chain.
0 commit comments