[英]React Hooks must be called in the exact same order in every component render error when building app - NextJs/Javascript
I am building a NextJs App and everything works well in dev.我正在构建一个 NextJs 应用程序,并且在开发中一切正常。 However when I am trying to build the application I get the errors:
但是,当我尝试构建应用程序时,出现错误:
./pages/genre.js/[genre].js ./pages/genre.js/[流派].js
15:14 Error: React Hook "useFetchTrendingCatagory" is called conditionally. 15:14 错误:有条件地调用 React Hook “useFetchTrendingCatagory”。 React Hooks must be called in the exact same order in every component render.
在每个组件渲染中,必须以完全相同的顺序调用 React Hooks。 Did you accidentally call a React Hook after an early return?
你是否在提前返回后不小心调用了 React Hook? react-hooks/rules-of-hooks
反应钩子/钩子规则
17:14 Error: React Hook "useFetchTopRatedCatagory" is called conditionally. 17:14 错误:有条件地调用 React Hook “useFetchTopRatedCatagory”。 React Hooks must be called in the exact same order in every component render.
在每个组件渲染中,必须以完全相同的顺序调用 React Hooks。 Did you accidentally call a React Hook after an early return?
你是否在提前返回后不小心调用了 React Hook? react-hooks/rules-of-hooks
反应钩子/钩子规则
19:14 Error: React Hook "useFetchMovieGenreResults" is called conditionally. 19:14 错误:有条件地调用 React Hook “useFetchMovieGenreResults”。 React Hooks must be called in the exact same order in every component render.
在每个组件渲染中,必须以完全相同的顺序调用 React Hooks。 Did you accidentally call a React Hook after an early return?
你是否在提前返回后不小心调用了 React Hook? react-hooks/rules-of-hooks
反应钩子/钩子规则
My code is below:我的代码如下:
import React from "react";
import { useRouter } from "next/router";
import useFetchMovieGenreResults from "../../hooks/useFetchMovieGenreResults";
import { useState } from "react";
import useFetchTrendingCatagory from "../../hooks/useFetchTrendingCatagory";
import useFetchTopRatedCatagory from "../../hooks/useFetchTopRatedCatagory";
const useMovies = (genre) => {
switch (genre) {
case 'Trending':
return useFetchTrendingCatagory()
case 'Top Rated"':
return useFetchTopRatedCatagory()
default:
return useFetchMovieGenreResults(genre)
}
}
export default function Genre () {
const router = useRouter();
const { genre } = router.query;
const mymovies = useMovies(genre)
return (
<div>
{/* <Navbar /> */}
<div>{genre}</div>
<Moviegenreresults movies={mymovies} />
</div>
)
}
Why is this error happening and is there a work-around for this error?为什么会发生此错误,是否有解决此错误的方法?
What's happening is exactly what the error says, useMovies
gets a different hook based on a condition which is the name of the genre, therefor the order of your hooks may be changed according to the arguments you pass to the useMovies
hook.发生的事情正是错误所说的,
useMovies
根据类型名称的条件获取不同的钩子,因此您的钩子的顺序可能会根据您传递给useMovies
钩子的 arguments 更改。
Instead of having a switch that is returning a different hook based on the genre
you passed make all the hook logic inside of one fe useMovies
hook and everything that needs to be modified (I assume for example the endpoint or the data you pass to that endpoint) pass it as your argument and make it work there.不是让一个开关根据您传递的
genre
返回一个不同的钩子,而是在一个 fe useMovies
钩子中创建所有钩子逻辑以及需要修改的所有内容(例如,我假设端点或传递给该端点的数据) 将其作为您的论点传递并使其在那里工作。
Technically the error is exactly how it sounds.从技术上讲,错误正是它听起来的样子。 You can't call hooks conditionally.
你不能有条件地调用钩子。 What you're doing in the intermediate useMovies function with the switch statement is conditionally calling a hook.
你在中间 useMovies function 和 switch 语句中所做的是有条件地调用一个钩子。 You may want to make that hook just something like useFetchMovies and just pass the genre into it, then inside of the hook itself do the logic that is happening in the useMovies function.
您可能希望像 useFetchMovies 一样制作该钩子并将类型传递给它,然后在钩子内部执行 useMovies function 中发生的逻辑。 Assuming you are just fetching from different endpoints in the different hooks you could do something like this, then return the fetcher function, or even use it in an effect if you really wanted to.
假设你只是从不同的钩子中的不同端点获取,你可以做这样的事情,然后返回 fetcher function,或者如果你真的想要的话,甚至可以使用它。
const useFetchMovies = (genre = null) => { const [movies, setMovies] = useState([]); const fetchMovies = async() => { let url; switch (genre) { case "Trending": url = "whatever the url you need is for this"; case 'Top Rated"': url = "whatever the url you need is for this"; default: url = "whatever the url you need is for this"; } try { const response = await fetch(url); console.log('response', response) setMovies(response); } catch (err) { console.error(err); throw err; } }; return { movies, setMovies, fetchMovies }; };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.