Skip to content

Commit 278a595

Browse files
committed
add doc for conditional fetch
1 parent db31714 commit 278a595

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ Don't expect it to grow in size, it is **feature complete**:
2020
- Can trigger manual refetch
2121
- Options to customize state updates
2222
- Can mutate state after fetch
23+
- Returned callbacks are stable
2324

2425
## Small size
2526

26-
- Tiny
2727
- Way smaller than popular alternatives
2828
- CommonJS + ESM bundles
2929
- Tree-shakable
@@ -299,6 +299,21 @@ const StarwarsHero = ({ id }) => {
299299
};
300300
```
301301

302+
#### How to handle conditional fetch?
303+
304+
You can enable/disable the fetch logic directly inside the async callback. In some cases you know your API won't return anything useful.
305+
306+
```tsx
307+
const asyncSearchResults = useAsync(async () => {
308+
// It's useless to call a search API with an empty text
309+
if (text.length === 0) {
310+
return [];
311+
} else {
312+
return getSearchResultsAsync(text);
313+
}
314+
}, [text]);
315+
```
316+
302317
#### How to have better control when things get fetched/refetched?
303318

304319
Sometimes you end up in situations where the function tries to fetch too often, or not often, because your dependency array changes and you don't know how to handle this.

0 commit comments

Comments
 (0)