【发布时间】:2020-10-18 14:03:45
【问题描述】:
所以每当我点击查看个人资料链接时
<Link to={`/profile/${_id}`} className="btn btn-primary">
View Profile
</Link>
它在 URL 中显示配置文件的用户 ID,这是好的。
但每当我单击它以将其与此按钮匹配时,我都会收到错误。
const Profile = ({ getProfileById, match }) => {
useEffect(() => {
getProfileById(match.params.id);
}, [getProfileById]);
return <div>test</div>;
};
我进入我的控制台
React Hook useEffect has a missing dependency: 'match.params.id'. Either include it or remove the dependency array
这就是我的应用 Js 中的内容。
<Route
exact
path="/profile/:id"
component={Profile}
/>
我认为它与我单击的按钮的 URL 不匹配。
在 Redux Devtools 中,它只返回一个配置文件错误。
【问题讨论】:
-
使用 useEffect 依赖数组参数旨在包含效果应监视更改以确定何时运行的变量。 linter 告诉你传递它
[match.params.id]。 -
我该怎么做?在我遵循的教程中,它具有相同的代码,但与它们配合得很好。
-
我总是使用这个评论(eslint-disable-next-line react-hooks/exhaustive-deps)并且工作正常。但这一个变得非常奇怪。
标签: reactjs react-redux react-router