Skip to content

Commit 0ff3937

Browse files
committed
Update readme for .caseWithAction()
1 parent 4f76278 commit 0ff3937

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ const reducer = reducerWithInitialState(INITIAL_STATE)
7777
Everything is typesafe. If the types of the action payload and handler don't line up, then
7878
TypeScript 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+
```
8096
The reducer builder chains are mutable. Each call to `.case()` modifies the callee to respond to the
8197
specified 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

174190
Mutates 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

179201
Returns a plain reducer function whose behavior matches the current state of the reducer chain.

0 commit comments

Comments
 (0)