Skip to content

Commit 523c88b

Browse files
committed
homework completed baby
1 parent 5b44571 commit 523c88b

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ Default value is set to false.
5959

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

62+
6. Homework (Solution on branch: feature/09-homework)
63+
64+
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`.
65+
66+
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.
67+
b.) Within the onError callback I want you to stop the polling.
68+
69+
Hint:
70+
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.
71+
6272
## Available Scripts
6373

6474
In the project directory, you can run:

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: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
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'
55

66
interface Props {}
77

88
const fetchSuperHeroes = () => {
9-
return axios.get('http://localhost:4000/superheroes1')
9+
return axios.get('http://localhost:4000/superheroes')
1010
}
1111

1212
const RQSuperHeroes: FC<Props> = (props: Props) => {
13+
const [interval, setInterval] = useState<any>(3000)
14+
1315
const onError = (error: Error) => {
1416
console.log('Perform side effect after encountering error', error)
17+
setInterval(false)
1518
}
1619

1720
const onSuccess = (response: any) => {
1821
console.log('Perform side effect after encountering error', response)
22+
if (response.data.length >= 4) setInterval(false)
1923
}
2024

21-
const { data, isLoading, isError, error, isFetching, refetch } = useQuery('super-heroes', fetchSuperHeroes, {
25+
const { data, isLoading, isError, error, isFetching } = useQuery('super-heroes', fetchSuperHeroes, {
2226
onError,
2327
onSuccess,
28+
refetchInterval: interval,
2429
})
2530

2631
console.log({ isLoading, isFetching })
@@ -36,9 +41,6 @@ const RQSuperHeroes: FC<Props> = (props: Props) => {
3641
return (
3742
<>
3843
<h2>RQ Super Heroes</h2>
39-
<button type="button" onClick={() => refetch()}>
40-
Fetch superheroes
41-
</button>
4244
<ul>
4345
{data?.data.map((hero: SuperHero, index: number) => (
4446
<li key={index}>{hero.name}</li>

0 commit comments

Comments
 (0)