useEffect(() => {
fetch(url)
.then(response => response.json())
.then(data => setData(data[0]))
.catch(err => console.warn(err))
}, [cityKey])
如何解决此警告?
useEffect(() => {
fetch(url)
.then(response => response.json())
.then(data => setData(data[0]))
.catch(err => console.warn(err))
}, [cityKey,url])
根据警告建议的解决方案,在依赖数组中包含url
。
useEffect(() => {
fetch(url)
.then(response => response.json())
.then(data => setData(data[0]))
.catch(err => console.warn(err))
}, [cityKey, url])
或者,您可以禁用警告(但不推荐(:
useEffect(() => {
fetch(url)
.then(response => response.json())
.then(data => setData(data[0]))
.catch(err => console.warn(err))
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [cityKey])