I was trying to add the async function in useEffect
. While doing this I am getting the warning about wrap fetchData
function in useCallback
hook. So, Where should I declare the useCallback
function, and how to implement it?
Here is the code:
import React, { useState, useEffect, useCallback } from 'react'
import './assets/main.css'
import ImageCard from './components/ImageCard'
import ImageSearch from './components/ImageSearch'
function App() {
const [images, setImages] = useState([])
const [isLoading, setIsLoading] = useState(true)
const [serachTerm, setSearchTerm] = useState('')
const fetchData = async () => {
try {
const data = await fetch(
`https://pixabay.com/api/?key=${process.env.REACT_APP_PIXABAY_API_KEY}&q=${serachTerm}&image_type=photo&pretty=true`
).then(res => res.json())
setImages(data.hits)
setIsLoading(false)
} catch (err) {
console.log(err)
}
}
useEffect(() => {
fetchData()
setIsLoading(false)
}, [setSearchTerm, fetchData])
The warning is about
fetchData
being redeclared each render cycle, thus retriggering theuseEffect
callback. TheuseCallback
memoizes the function to provide a stable reference.You have two options:
Memoize
fetchData
withuseCallback
Move
fetchData
into theuseEffect
hook so its reference doesn't matter