Skip to content

Commit 3f05735

Browse files
committed
documentation and corrections
1 parent 2b416b1 commit 3f05735

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

README.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,72 @@ npm install --save redux-bundler-hook redux-bundler-async-resources redux-bundle
1212

1313
## Usage
1414

15+
(assuming you've used `createAsyncResourceBundle` for "hotCarDeals" and `createAsyncResourcesBundle` for "carDeals" on a specific car id)
1516

16-
... throwing should be approached with care – hooks rules
17+
```javascript
18+
import React from 'react'
19+
import { useConnect } from 'redux-bundler-hook'
20+
import { useAsyncResource, useAsyncResourcesItem } from 'redux-bundler-async-resources-hooks'
21+
22+
// ... other imports
23+
24+
export default function MyAutomarketDashboard() {
25+
const { currentCarId } = useConnect('selectCurrentCarId')
26+
27+
const { hotCarDeals, hotCarDealsIsLoading, hotCarDealsError } = useAsyncResource('hotCarDeals')
28+
29+
const {
30+
item: currentCarDeals,
31+
itemIsLoading: currentCarDealsLoading,
32+
itemError: currentCarDealsError,
33+
} = useAsyncResourcesItem('carDeals', currentCarId)
34+
35+
return (
36+
<>
37+
<h1>Here are some deals for you on this car:</h1>
38+
<CarDealsList deals={currentCarDeals} loading={currentCarDealsLoading} error={currentCarDealsError} />
39+
<h1>Also take a look at these hot deals:</h1>
40+
<CarDealsList deals={hotCarDeals} loading={hotCarDealsIsLoading} error={hotCarDealsError} />
41+
</>
42+
)
43+
}
44+
```
45+
46+
Hooks above will provide you all fields matching selectors that bundles are capable of.
47+
48+
## Using with Suspense and Error Boundaries
49+
50+
**Warning: using Suspense or Error boundaries assumes throwing things. Make sure these hooks are the only ones or the last ones you use within a single component in order to respect [Rules of Hooks](https://reactjs.org/docs/hooks-rules.html).**
51+
52+
Both hooks receive one last `settings` parameter which can have following fields (all disabled by default):
53+
54+
- `throwErrors` – if truthy, will throw an error for you when there is an error and there is no past data to show. Setting it to `"always"` will always throw if there is an active error.
55+
- `throwPromises` – if truthy, will throw a promise when item is loading and there is no data to show. Setting it to "always" will always throw a promise if item is loading or refreshing.
56+
- `eagerFetch` – see below
57+
58+
##### Using errors
59+
60+
Error boundary can implement a proper "wait-retry" mechanism. Instead of throwing original error, hook will throw an instance of `AsyncResourceError` which will have following properties:
61+
62+
- `resourceName` – which should be self-explanatory
63+
- `originalError` – an instance of original exception that bundle's `getPromise` rejected with
64+
- `permanent` – translated from original error
65+
- `retryAt` – a timestamp at which bundle will attempt next retry for this item
66+
- `retry` – method that can force-trigger a fetch on an item
67+
68+
##### Enabling default behavior
69+
70+
Default values for `throwErrors` and `throwPromises` can be overridden per hook. If you prefer to always use error boundaries and/or suspenses, you can do the following early before any react code is on:
71+
72+
```javascript
73+
import { useAsyncResource, useAsyncResourcesItem } from 'redux-bundler-async-resources-hooks'
74+
75+
useAsyncResource.defaults = { throwErrors: true, throwPromises: true }
76+
useAsyncResourcesItem.defaults = { throwErrors: true, throwPromises: true }
77+
```
78+
79+
## Eager fetch
80+
81+
When setting `eagerFetch` to a truthy value in hooks `settings` parameter, hook will fire an effect which would trigger fetch actions on the item when it is pending for fetch.
82+
83+
It is on-purpose impossible to override with setting with a default.

src/AsyncResourceError.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default function AsyncResourceError(name, originalError, { permanent, ret
55

66
this.name = 'AsyncResourceError'
77
this.message = message
8+
this.resourceName = name
89
this.originalError = originalError
910
this.permanent = Boolean(permanent)
1011
this.retryAt = retryAt

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ export { default as useAsyncResourceKeys } from './useAsyncResourceKeys'
33

44
export { default as useAsyncResourcesItem } from './useAsyncResourcesItem'
55
export { default as useAsyncResourcesKeys } from './useAsyncResourcesKeys'
6+
7+
export { default as AsyncResourceError } from './AsyncResourceError'

0 commit comments

Comments
 (0)