Skip to content

Commit 6d2f435

Browse files
authored
Improvements to consistency (#152)
* lint fix * improve docs * minor improvement to types * rationalise manage-resource-state into a smaller number of api functions with clear abstraction * consistent API for all actions of resource and context arguments * more precise variable names for slices * clearly demarcate private and public actions and stub public action dependencies * minor fix to test label for consistency * ensure resource slice always represents correct future state, docs, tests * reinstate async function
1 parent 2c020ba commit 6d2f435

File tree

15 files changed

+1626
-855
lines changed

15 files changed

+1626
-855
lines changed

docs/api/hooks.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,24 @@ export const Feed = () => {
3535

3636
As well as returning actions that act on the resource (i.e. update and refresh), `useResource` returns four different properties that indicate the state of the resource. These four are `data`, `loading`, `error` and `promise`. `useResource` will return different combinations of these four properties depending on the state of the resource. The table below shows all the possible combinations.
3737

38-
| state | data | loading | error | promise |
39-
| :-----------------------: | :--------: | :-----: | :-----------: | :-----: |
40-
| idle | null | false | null | null |
41-
| loading | null or {} | true | null or Error | Promise |
42-
| loading after ssr timeout | null | true | TimeoutError | null |
43-
| fetch successful | {} | false | null | Promise |
44-
| fetch error | null or {} | false | Error | Promise |
45-
46-
It is important to note that loading can be true even when there is an error. In that case, promise will be null because there is no Suspense support on the server. Developers should give priority to loading when deciding between loading or error states for their components. Promises/errors should only ever be thrown on the client
47-
48-
It also acceps some options as second argument to customise the behaviour, like `routerContext`.
38+
| state | data | loading | error | promise |
39+
| :----------------: | :------: | :-----: | :----------: | :-------------------------------: |
40+
| initial | null | false | null | null |
41+
| data (from SSR) | {} | false | null | Promise (resolves `data`) |
42+
| timeout (from SSR) | null | true | TimeoutError | null |
43+
| async loading | _-prev-_ | true | _-prev-_ | Promise (pending) |
44+
| async successful | {} | false | null | Promise (resolves `data`) |
45+
| async error | _-prev-_ | false | Error | Promise (rejects `error`) |
46+
| cleared | null | false | null | null |
47+
| updated {} | {} | false | null | Promise (resolves updated `data`) |
48+
49+
Where `-prev-` indicates the field will remain unchanged from any previous state, possibly the inital state.
50+
51+
It is important to note
52+
* The timeout state is essentially a hung loading state, with the difference that `promise = null` and `error != null`. Developers should give priority to `loading` when deciding between loading or error states for their components. Promises/errors should only ever be thrown on the client.
53+
* The `promise` reflects the last operation, either async or explicit update. Update will clear `error`, set `data`. It will also set a `promise` consistent with that `data` so long as no async is `loading`. When `loading` the `promise` will always reflect the future `data` or `error` from the pending async.
54+
55+
Additionaly `useResource` accepts additional arguments to customise behaviour, like `routerContext`.
4956
Check out [this section](../resources/usage.md) for more details on how to use the `useResource` hook.
5057

5158
## useRouter

0 commit comments

Comments
 (0)