Skip to content

Commit

Permalink
refactor: remove async function inside useEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
zigsong committed Sep 2, 2021
1 parent a2462e7 commit 23b1e7b
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/pages/Search/Search.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import React, { useState, useEffect, useRef } from "react";
import { MdSearch } from "react-icons/md";

import { fetchTrendingGifs, fetchGifsByKeyword } from "../../service/fetchGif";

import NavBar from "../../components/NavBar/NavBar";
import Footer from "../../components/Footer/Footer";
import GifItem from "../../components/GifItem/GifItem";

import { fetchTrendingGifs, fetchGifsByKeyword } from "../../service/fetchGif";
import styles from "./Search.module.css";

const DEFAULT_PAGE_INDEX = 0;
Expand All @@ -18,7 +16,7 @@ const ResultTitle = ({ showTrending, noResult }) => {

if (showTrending) {
return <h4 className={styles.resultTitle} >
🔥 <span>Trending Now</span> 🔥
🔥 <span>Trending Now</span> 🔥
</h4 >;
}

Expand Down Expand Up @@ -71,21 +69,25 @@ const Search = () => {
}

const loadMore = async () => {
const nextPageIndex = currentPageIndex + 1;
const gifs = await fetchGifsByKeyword(searchKeyword, nextPageIndex);
const nextPageIndex = currentPageIndex + 1;
const gifs = await fetchGifsByKeyword(searchKeyword, nextPageIndex);

setGifList([...gifList, ...gifs]);
setCurrentPageIndex(nextPageIndex);
setGifList([...gifList, ...gifs]);
setCurrentPageIndex(nextPageIndex);
}

useEffect(async () => {
if (loading) {
const gifs = await fetchTrendingGifs();

setGifList(gifs);
setLoading(false);
useEffect(() => {
const loadTrendingGifs = async () => {
if (loading) {
const gifs = await fetchTrendingGifs();

setGifList(gifs);
setLoading(false);
}
}

loadTrendingGifs();

return () => setLoading(true);
}, []);

Expand Down

0 comments on commit 23b1e7b

Please sign in to comment.