Skip to content

Commit 7e513a3

Browse files
authored
Update README.md
1 parent e476c69 commit 7e513a3

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,24 @@ const asyncSomething = useAsync(() => fetchSomething(state.a, state.b), [state.a
268268
Note you can also use this to "build" a more complex payload. Using `useMemo` does not guarantee the memoized value will not be cleared, so it's better to do:
269269

270270
```tsx
271-
const asyncSomething = useAsync(() => fetchSomething(buildFetchPayload(state)), [state.a, state.b, state.whateverNeedToTriggerRefetch]);
271+
const asyncSomething = useAsync(async () => {
272+
const payload = buildFetchPayload(state);
273+
const result = await fetchSomething(payload);
274+
return result;
275+
}), [state.a, state.b, state.whateverNeedToTriggerRefetch]);
276+
```
277+
278+
You can also use `useAsyncCallback` to decide yourself manually when a fetch should be done:
279+
280+
```tsx
281+
const asyncSomething = useAsyncCallback(async () => {
282+
const payload = buildFetchPayload(state);
283+
const result = await fetchSomething(payload);
284+
return result;
285+
}));
286+
287+
// Call this manually whenever you need:
288+
asyncSomething.execute();
272289
```
273290

274291

0 commit comments

Comments
 (0)