Skip to content

Commit 2653f8d

Browse files
authored
Merge branch 'master' into feature/10-data-transformation
2 parents e0510a2 + 0aa9251 commit 2653f8d

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

README.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# React Query tutorial
22

3-
Tutorial videos from made by [Codevolution](https://www.youtube.com/watch?v=Ev60HKYFM0s&list=PLC3y8-rFHvwjTELCrPrcZlo6blLBUspd2&index=3).
3+
Tutorial videos are made by [Codevolution](https://www.youtube.com/watch?v=Ev60HKYFM0s&list=PLC3y8-rFHvwjTELCrPrcZlo6blLBUspd2&index=3).
44

55
Code is separated with different branches. Every video has his own branch.
66

@@ -16,7 +16,9 @@ Code is separated with different branches. Every video has his own branch.
1616

1717
## About react-query
1818

19-
1. react-query cache
19+
Link to [offical website](https://react-query.tanstack.com/).
20+
21+
**1. react-query cache**
2022

2123
The first time useQuery is fired for superheros key, isLoading flag is set to true and network request is send to fetch the data. When the request is completed it is cached used the query key (superheroes) and the fetchSuperHeroes function as the unique identifiers.
2224

@@ -31,7 +33,7 @@ React query cache the data so we don't need to see the loading indicator every t
3133

3234
The default cache time is set to 5 mintes. We can change it with options cacheTime property
3335

34-
2. react-query stale time
36+
**2. react-query stale time**
3537

3638
Let's say the data doesn't change that often. We can set the stale time to 30 seconds and the user will see the cached query results or cached data for 30 seconds before refetching it again. The default state time is set to 0 seconds.
3739

@@ -49,7 +51,7 @@ If set to true, the query will refetch on window focus if the data is stale. If
4951

5052
Default value is set to true.
5153

52-
5. react-query polling
54+
5. react-query refetchInterval - Polling
5355

5456
Fetching data at regular intervals. For example: If you have component that shows real time price of different stocks, you might want to fetch the data every second so is in sync with UI.
5557

@@ -59,7 +61,17 @@ Default value is set to false.
5961

6062
Polling is paused when the window lose focus. To fix that issue we can set `refetchIntervalInBackground` property to true.
6163

62-
6. react-query select - Data Transformation
64+
6. Homework (Solution on branch: feature/09-homework)
65+
66+
Combine polling with callbacks. Use the `refetchInterval` option to pull the api data every 3 seconds. Behind the scenes add a fourth superhero of your choice to the superheroes array in `db.json`.
67+
68+
a.) Within the onSuccess callback check if the number of heroes is 4 and ifit is the case I want you to stop the polling.
69+
b.) Within the onError callback I want you to stop the polling.
70+
71+
Hint:
72+
Mantain state variable whose initial value is 3000. State variable will be assigned to `refetchInterval` configuration. In callbacks check for the response / errors and set the state variable to false.
73+
74+
7. react-query select - Data Transformation
6375

6476
For data tranformation react-query provides us with `select` flag.
6577

db.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
"id": 3,
1515
"name": "Wonder Woman",
1616
"alterEgo": "Princess Diana"
17+
},
18+
{
19+
"id": 4,
20+
"name": "Spiderman",
21+
"alterEgo": "Nejc Rogelšek"
1722
}
1823
]
1924
}

src/pages/RQSuperHeroes/RQSuperHeroes.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC } from 'react'
1+
import { FC, useState } from 'react'
22
import { useQuery } from 'react-query'
33
import axios from 'axios'
44
import { SuperHero } from 'interfaces'
@@ -12,13 +12,15 @@ const fetchSuperHeroes = () => {
1212
const RQSuperHeroes: FC<Props> = (props: Props) => {
1313
const onError = (error: Error) => {
1414
console.log('Perform side effect after encountering error', error)
15+
setInterval(false)
1516
}
1617

1718
const onSuccess = (response: any) => {
1819
console.log('Perform side effect after encountering error', response)
20+
if (response.data.length >= 4) setInterval(false)
1921
}
2022

21-
const { data, isLoading, isError, error, isFetching, refetch } = useQuery('super-heroes', fetchSuperHeroes, {
23+
const { data, isLoading, isError, error, isFetching } = useQuery('super-heroes', fetchSuperHeroes, {
2224
onError,
2325
onSuccess,
2426
select: (response) => {
@@ -40,9 +42,6 @@ const RQSuperHeroes: FC<Props> = (props: Props) => {
4042
return (
4143
<>
4244
<h2>RQ Super Heroes</h2>
43-
<button type="button" onClick={() => refetch()}>
44-
Fetch superheroes
45-
</button>
4645
<ul>
4746
{data.map((heroName: string, index: number) => (
4847
<li key={index}>{heroName}</li>

0 commit comments

Comments
 (0)