制作一个视频游戏剪辑发布应用程序。我有一个控制器在后端,这将给我的剪辑的顺序,大多数评论剪辑到最少。我希望在数据正常排序之间进行切换,通过使用select标记(稍后我将添加更多过滤器)从大多数到最少的注释。我希望能够根据select标记onChange中的状态有条件地呈现这两个获取,但是我得到了一个错误。
Line 18:5: React Hook "useEffect" is called conditionally. React Hooks must be called in the exact same order in every component render react-hooks/rules-of-hooks
Line 24:5: React Hook "useEffect" is called conditionally. React Hooks must be called in the exact same order in every component render react-hooks/rules-of-hooks
这是我的密码。注意,我只是在用布尔值测试它。
const boolean = true;
if (boolean == true) {
useEffect(() => {
fetch("/clips")
.then((r) => r.json())
.then((data) => setClipData(data));
}, []);
} else {
useEffect(() => {
fetch("/most_comments")
.then((r) => r.json())
.then((data) => setClipData(data));
}, []);
}
发布于 2022-05-24 23:11:43
正如错误所述,不能有条件地调用useEffect
。相反,不管调用它一次,在该函数中,查看是否需要获取剪辑或注释。
useEffect(() => {
if (boolean) {
fetch("/clips")
.then((r) => r.json())
.then((data) => setClipData(data));
// don't forget to .catch errors here
} else {
fetch("/most_comments")
.then((r) => r.json())
.then((data) => setClipData(data)); // did you mean to call setCommentsData or something here?
// don't forget to .catch errors here
}
}, []);
https://stackoverflow.com/questions/72370360
复制相似问题