You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Usage with redux-thunk](#usage-with-redux-thunk)
29
29
-[Usage with redux-saga](#usage-with-redux-saga)
30
30
-[Example with everything](#example-with-everything)
31
+
-[Advanced](#advanced)
31
32
-[API](#api)
32
33
-[Other similar libraries](#other-similar-libraries)
33
34
-[Caveats](#caveats)
@@ -63,6 +64,8 @@ in a more modular way while providing some additional utilities such as [depende
63
64
64
65
## Usage
65
66
67
+
Reducktion minimizes the amount of boilerplate by firstly skipping manually defining action types entirely, and secondly merging actions and reducers together.
Nowadays, many websites are SPAs (Single Page Applications) and have to get some data from an API to show to the users.
488
+
This data fetching process usually consists of three stages: `loading the data`, `receiving the data`, and `handling errors`.
489
+
490
+
Reducktion provides some higher level helpers to make handling any API related actions
491
+
less laborious.
492
+
493
+
Let's first see what we would normally need to create the necessary things for fetching some imaginary **orders** and taking into account the three stages mentioned earlier.
That's quite a lot of setup / boilerplate for handling three stages of our API call.
541
+
542
+
Reducktion provides a helper action creator called `createApiAction` that can be used to create the same three actions as above baked into one enhanced action that behind the scenes updates the necessary state fields automatically by creating the individual reducers for you.
543
+
544
+
The simplest way to use `createApiAction` is to just give it the name of state field (eg. **orders**) where you want to save the data.
545
+
By default the auto-created reducers will also update three fields during the different stages of the API call.
However, in case you need more control over your state fields for the different stages or want to add your own you can achieve it by giving `createApiAction` a reducer definition object.
593
+
594
+
> NOTE: if you choose to use reducer definition object you always **have to** provide the `success` reducer! Other reducers are optional.
595
+
596
+
```js
597
+
constduck=createDuck({
598
+
// ...
599
+
actions: () => ({
600
+
fetchOrders:createApiAction({
601
+
// REQUIRED!
602
+
success: (state, action) => ({
603
+
...state,
604
+
orders:action.payload,
605
+
isLoading:false,
606
+
}),
607
+
// optional
608
+
loading: (state, action) => ({
609
+
...state,
610
+
isLoading:true,
611
+
hasError:false,
612
+
error:null,
613
+
}),
614
+
// optional
615
+
failure: (state, action) => ({
616
+
...state,
617
+
isLoading:false,
618
+
hasError:true,
619
+
error:action.payload,
620
+
}),
621
+
}),
622
+
}),
623
+
// ...
624
+
});
625
+
```
626
+
479
627
## API
480
628
629
+
> TODO: UPDATE API DOCS!
630
+
481
631
Check out the more detailed [API documentation](API.md).
0 commit comments