Skip to content

Commit 8a5371d

Browse files
committed
docs(README): add useWorkerLoad example
1 parent d53b892 commit 8a5371d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,28 @@ const { isLoading, error, result, callback } = useWorker(
9292
`isLoading` is set to true as soon as the callback is loaded and only returns to `false` when it
9393
ends or when an error happens. If an exception is thrown or a promise fails, `error` will be updated.
9494

95+
### Worker Load
96+
97+
This is a worker that starts loading immediately and stores the result in a state. Useful for
98+
loading data when you render a component:
99+
100+
```ts
101+
const { isLoading, error, data } = useWorkerLoad(
102+
async () => {
103+
return await getUserName();
104+
},
105+
'no-name', // data's initial value
106+
);
107+
```
108+
109+
If the worker fails, the error is returned in the `error` state with a retry function:
110+
111+
```ts
112+
const { error } = useWorkerLoad(...);
113+
114+
error?.value // the actual Error object
115+
error?.retry() // calls the worker again
116+
```
95117

96118
### Did Mount
97119

0 commit comments

Comments
 (0)