有条件地呈现使用效果提取反应
制作视频游戏剪辑发布应用程序。我有一个在后端的控制器,可以为我提供剪辑,按照对剪辑的大多数评论的顺序至少。我想通过使用SELECT标签在正常订购的数据之间切换到大多数评论(以后我会添加更多过滤器)。我希望我可以根据选择标签的状态有条件地渲染两个提取物,但是我有一个错误。
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));
}, []);
}
Making a video game clip posting app. I have a controller on the backend that will give me the clips in order of most commented on clips to least. I would like to switch between having the data ordered normally, to from most to least comments by using a select tag (later I will add more filters). I was hoping I could conditionally render both of the fetches based on a state from the select tag onChange, but I got an error.
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
this is my code. Note that I was just testing it with a boolean.
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));
}, []);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如错误所说,您无法有条件地调用
useffect
。相反,无论如何,在该功能内部都将其称为一次,请查看您是否需要获取剪辑或评论。As the error says, you can't have
useEffect
called conditionally. Instead, call it a single time regardless, and inside that function, look to see whether you need to fetch the clips or the comments.