Skip to content

Commit 91318f1

Browse files
committed
refactor v5 tanstack query links
1 parent 31cc7cc commit 91318f1

File tree

1 file changed

+50
-27
lines changed

1 file changed

+50
-27
lines changed

challenges/data/02.md

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,25 @@ Here's how it works:
3030
```javascript
3131
// DO NOT MONKEY COPY/PASTE ME
3232

33+
/**
34+
* Fetches data from an API endpoint and returns the JSON response.
35+
* @returns {Promise<Object>} A promise that resolves to the JSON response from the API.
36+
*/
3337
async function getDataFromApi() {
34-
const result = await fetch(`https://api.example.com/collection/`);
35-
const json = await result.json();
36-
return json;
38+
try {
39+
const response = await fetch(`https://api.example.com/collection/`);
40+
const json = await response.json();
41+
return json;
42+
} catch (error) {
43+
throw new Error("Something bad happened with the api…");
44+
}
3745
}
3846

39-
function Component({ itemId }) {
40-
const { isLoading, isError, data } = useQuery(
41-
["dataFromApi"],
42-
getDataFromApi
43-
);
47+
/**
48+
* Renders a component that fetches data from an API and displays it.
49+
*/
50+
function Component() {
51+
const { isLoading, isError, data } = useQuery(["dataFromApi"], getDataFromApi);
4452

4553
if (isLoading) {
4654
return <Text>Loading…</Text>;
@@ -69,50 +77,65 @@ npm install @tanstack/react-query
6977

7078
- [ ] Add a `QueryClientProvider` and wrap the application entry point.
7179

72-
**🔭 Hint:** You can have a look at the [`QueryClientProvider` documentation](https://tanstack.com/query/v4/docs/reference/QueryClientProvider)
80+
**🔭 Hint:** You can have a look at the [`QueryClientProvider` documentation](https://tanstack.com/query/latest/docs/framework/react/reference/QueryClientProvider#queryclientprovider)
7381

7482
### Fetching resources across the network
7583

76-
- [ ] Take 5 min to **read carefully** the [`useQuery` documentation](https://tanstack.com/query/v4/docs/guides/queries).
77-
78-
In the example `useQuery`, `isLoading` and `isError` lives on the same files.
79-
80-
To have a more robust application, and separte the concerns of the data and the ui, we can wrap our queries into one custom hook `useStarships()`.
84+
- [ ] Take 5 min to **read carefully** the [`useQuery` documentation](https://tanstack.com/query/latest/docs/framework/react/guides/queries).
8185

8286
The logic is:
8387

8488
1. `fetch` data from the endpoint and return the result as a `json`
8589
1. Get the result from the fetch and return the result of `useQuery`
86-
1. Use our custom hook to render the UI
90+
1. render the UI
91+
92+
Using the `useQuery` hook and `fetchStarshipsData`, fetch data from the Star Wars API:
93+
94+
```javascript
95+
async function fetchStarshipsData() {
96+
try {
97+
const response = await fetch(`https://swapi.py4e.com/api/starships/`);
98+
const json = await response.json();
99+
return json;
100+
} catch (error) {
101+
throw new Error("Something bad happened with the api…");
102+
}
103+
}
104+
```
105+
106+
- [ ] Use the hook to **dislay a loading message**.
107+
- [ ] Use the hook to **handle errors**.
108+
- [ ] Use the hook to replace `data.json` and **render data** with a `FlatList`.
109+
110+
## 👽 Bonus
111+
112+
In the previous challenge `useQuery`, `isPending` and `isError` lives on the same files. To have a more robust application, and separte the concerns of the data and the ui, we can wrap our queries into one custom hook `useStarships()`.
87113

88114
- [ ] Create a new file `useStarships.ts` and paste the content bellow
89115

90116
```javascript
91117
// ./src/hooks/useStarships.ts
92118
import { useQuery } from "@tanstack/react-query";
93119

94-
async function fetchData() {
95-
const result = await fetch(`https://swapi.py4e.com/api/starships/`);
96-
const json = await result.json();
97-
return json;
120+
async function fetchStarshipsData() {
121+
try {
122+
const response = await fetch(`https://swapi.py4e.com/api/starships/`);
123+
const json = await response.json();
124+
return json;
125+
} catch (error) {
126+
throw new Error("Something bad happened with the api…");
127+
}
98128
}
99129

100130
export function useStarships() {
101-
return useQuery({ queryKey: ["starships"], queryFn: fetchData} );
131+
return useQuery({ queryKey: ["starships"], queryFn: fetchStarshipsData} );
102132
}
103133
```
104134

105135
Now you can write `useStarships()` anywhere in your Screen and React Query handles the rest.
106136

107-
- [ ] Go to your Feed screen and remplace the `data.json` with data from the swapi.
108-
- [ ] Use `useStarships` hook to **dislay a loading message**.
109-
- [ ] Use `useStarships` hook to **handle errors**.
110-
- [ ] Use `useStarships` hook to **render data** with a `FlatList`.
111-
112137
If you want to display some images on your `Card`, you can use this placeholder `https://picsum.photos/400/200` as a `source`.
113138

114-
## 👽 Bonus
115-
116139
- [ ] Save this talk to your "Watch later" playlist: [Custom Hooks in React: The Ultimate UI Abstraction Layer](https://www.youtube.com/watch?v=J-g9ZJha8FE) from Tanner Linsley
117140
- [ ] Display a nice animated placeholder during loading with [rn-placeholder](https://github.com/mfrachet/rn-placeholder)
118141
- [ ] Add a "Pull to Refresh" functionality to your FlatList with `onRefresh`

0 commit comments

Comments
 (0)