"use client"
import SearchResults from "./searchResults";
import axios from "axios";
import { useSelector } from "react-redux";
const getRecipe = async () => {
const response = await axios.get(
`https://api.edamam.com/api/recipes/v2?type=public&q=cake&app_id=API_ID&app_key=API_KEY`
);
const data = await response.data;
return data;
};
const SearchRecipe = async () => {
const recipes = await getRecipe();
const searchTerm = useSelector((state) => state.recipe.value.searchTerm);
return (
<div>
</div>
);
};
export default SearchRecipe;
字符串
const searchTerm = useSelector((state)=> state.recipe.value.searchTerm);在这里,我试图从我使用redux工具包创建的切片中访问searchTerm状态,但当我运行此组件时错误:无法读取null的属性(阅读“useContext”)此错误表明它在另一个组件中工作正常。
1条答案
按热度按时间vjrehmav1#
不能将异步组件用于客户端代码。异步组件用于服务器端组件。
此外,在您的情况下,您应该使用
useEffect
来获取数据。