Skip to content

Commit 650a216

Browse files
committed
[ADD]🚀 useFetchGifs added
1 parent 2da4d56 commit 650a216

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

‎src/components/GifGrid.js‎

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
1-
//import React, {useState, useEffect} from 'react';
21
import React from 'react';
32

43
import { useFetchGifs } from '../hooks/useFetchGifs';
54

6-
//import GifGridItem from './GifGridItem';
7-
//import { getGifs } from '../helpers';
5+
import GifGridItem from './GifGridItem';
86

97
const GifGrid = ({category}) => {
10-
//const [gifs, setGifs] = useState([]);
11-
12-
const {loading} = useFetchGifs();
13-
14-
//useEffect(() => {
15-
// getGifs(category)
16-
// .then(setGifs)
17-
//}, [category]);
8+
const {data: gifs, loading} = useFetchGifs(category);
189

1910
return(
2011
<div className="card-grid">
2112
<h3>{category}</h3>
13+
{ loading && <p>Loading</p> }
2214
{
23-
loading ?
24-
'loading...'
25-
:
26-
'show'
27-
}
28-
{/*
2915
gifs.map(gif => (
3016
<GifGridItem
3117
key={gif.id}
3218
{...gif}
3319
/>)
3420
)
35-
*/}
21+
}
3622
</div>
3723
);
3824
}

‎src/hooks/useFetchGifs.js‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import {useState} from 'react';
1+
import { useState, useEffect } from 'react';
22

3-
export const useFetchGifs = () => {
3+
import { getGifs } from '../helpers';
4+
5+
export const useFetchGifs = category => {
46
const [state, setState] = useState({
57
data: [],
68
loading: true
79
});
810

9-
setTimeout(() => {
10-
setState({
11-
...state,
12-
loading: false
13-
})
14-
}, 3000);
11+
useEffect(() => {
12+
getGifs(category)
13+
.then(gifs => setState({
14+
data: gifs,
15+
loading: false
16+
}))
17+
},[category]);
1518

1619
return state; // { data: [], loading: true }
1720
}

0 commit comments

Comments
 (0)