【发布时间】:2021-10-31 20:46:40
【问题描述】:
我已将 id 作为状态一函数传递
<Link
className="title"
id="titles"
to={{
pathname: "/postDetail",
state: { id:story.objectID }
}}
>
{story.title}
</Link>
下面是子函数
const location = useLocation();
const { id } = location.state;
useEffect(() => {
const link = `http://hn.algolia.com/api/v1/items/${id}`
const getPost = async () => {
await fetch(link)
.then((response) => response.json())
.then((data) => {
const post = data;
setPost(post);
setLoading(false);
});
}
getPost();
}, []);
我不知道我做错了什么有人说要移动const { id } = location.state;
在 useEffect() 里面。我试过了,但它仍然给我同样的错误。
怎么办?
【问题讨论】:
标签: javascript reactjs api react-router react-hooks