1 回答

TA贡献1772条经验 获得超5个赞
当你调用 React 时,它表明这是对这个 useEffect Hook 的依赖。useEffectgetRecipes();getRecipes
您可以使用效果进行更新:
useEffect(() => {
getRecipes();
}, [query, getRecipes]);
但是,您将获得
The 'getRecipes' function makes the dependencies of useEffect Hook (at line 18) change on every render. Move it inside the useEffect callback. Alternatively, wrap the 'getRecipes' definition into its own useCallback() Hook. (react-hooks/exhaustive-deps)
因此,您可以更新为:
useEffect(() => {
const getRecipes = async () => {
const response = await fetch(
`https://api.edamam.com/search?q=${query}&app_id=${APP_ID}&app_key=${APP_KEY}`
);
const data = await response.json();
setRecipes(data.hits);
console.log(data.hits);
};
getRecipes();
}, [query]);
这表示在修改时将调用此效果,这意味着 getRecipes 使用 调用 API。queryquery
添加回答
举报