我试图在useEffect
. 在执行此操作时,我收到有关钩子中的 wrapfetchData
函数的警告。useCallback
那么,我应该在哪里声明useCallback
函数,以及如何实现呢?
这是代码:
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])