Skip to content
This repository has been archived by the owner on Feb 3, 2020. It is now read-only.

Commit

Permalink
docs: simplify and clarify advanced createAction examples
Browse files Browse the repository at this point in the history
  • Loading branch information
knpwrs committed Feb 16, 2019
1 parent 1bebb1d commit 9c817f0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,18 @@ The action creator function will be typed to take whatever you provide as a
payload type.

If your action creator needs to take arguments other than whatever your payload
is typed as you can provide types in the generic signature:
is typed as you can simply provide a typed payload creator function:

```ts
// a, b, and c are inferred below:
const addThreeNumbers = createAction<number, [number, number, number]>('ADD_THREE_NUMBERS', (a, b, c) => a + b + c);
// addThreeNumbers accepts three ordinal number aguments and has a number payload:
const addThreeNumbers = createAction('ADD_THREE_NUMBERS', (a: number, b: number, c: number) => a + b + c);
```

If you need to customize the [SFP] `meta` property you can supply a second meta
customizer function:
creator function:

```ts
const addThreeNumbers = createAction<number, [number, number, number], number>(
const addThreeNumbers = createAction<number, [number, number, number], string>(
'ADD_THREE_NUMBERS',
// Create `payload`
(a, b, c) => a + b + c,
Expand All @@ -131,6 +131,11 @@ const addThreeNumbers = createAction<number, [number, number, number], number>(
);
```

Note that the payload and meta creators must accept the same arguments, but can
return different types. In the example above the payload creator takes three
numbers and returns a number while the meta creator takes three numbers and
returns a string.

### `handleAction(actionCreator, (state: Draft<State>, payload) => void, initialState?: State)`

The `handleAction` function returns a single reducer function. The first
Expand Down
25 changes: 25 additions & 0 deletions src/create-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ test('meta customizer', () => {
});
});

test('generic inference', () => {
const ac = createAction('generic-inference', (a: number, b: number, c: number) => a + b + c);
expect(ac.type).toBe('generic-inference');
expect(ac(1, 2, 3)).toEqual({
type: 'generic-inference',
payload: 6,
});
});

test('generic inference with meta', () => {
const ac = createAction<number, [number, number, number], string>(
'generic-inference-with-meta',
// Create `payload`
(a, b, c) => a + b + c,
// Create `meta`
(a, b, c) => `${a} + ${b} + ${c}`,
);
expect(ac.type).toBe('generic-inference-with-meta');
expect(ac(1, 2, 3)).toEqual({
type: 'generic-inference-with-meta',
payload: 6,
meta: '1 + 2 + 3',
});
});

test('error payload', () => {
const ac = createAction<Error>('with-error');
expect(ac.type).toBe('with-error');
Expand Down

0 comments on commit 9c817f0

Please sign in to comment.