Replies: 7 comments 13 replies
-
|
Hello!
So in most cases, two Very simple example: import { useRouter } from "next/router";
const Home = () => {
const router = useRouter();
const router2 = useRouter();
console.log(router === router2); // will be true
return <div />;
};
export default Home;That being said, what is the actual purpose of the eslint rule ? The rule should be here as a warning to inform you that you may have made a mistake. The dependencies array of In your case, do you want your effect to run whenever the I believe the dependencies array should be different for every use-case. Perhaps you want to redirect the user if the Or you only want to do something once (on mount), like an API call? Your array will be empty. Even if the eslint rule doesn't like it: useEffect(() => {
// Do something
}, [])This is a personal opinion, but eslint rules should help a developper do his job, it should not make his life harder. But to be honest, in your example, it doesn't really matter 😅 . |
Beta Was this translation helpful? Give feedback.
-
|
I stumbled across this discussion when noticing a bug in my app due to expecting a useEffect(() => {
AutoUpdateController.initialize(router);
}, [router]);This To debug referentially stable values between renders, you can test using a // usePrevious.ts
import { useEffect, useRef } from 'react';
export default function usePrevious<T>(value: T) {
const ref = useRef<T>();
useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
}and in your component do something like: const router = useRouter();
const prevRouter = usePrevious(router);
if (prevRouter) {
console.log(router === prevRouter);
}Which makes it clear that the value of IMO this is a pretty big footgun that should be called out. Assuming it's not feasible to actually make There's even a Potential ESLint errors section of the docs! Seems like a great place to mention this rules-of-hooks ESLint error that everyone will get. (cc: @leerob) |
Beta Was this translation helpful? Give feedback.
-
|
Has anyone found a solution to this problem? |
Beta Was this translation helpful? Give feedback.
-
|
Well, Infinite loop here when ading router to my useEffect dependency. I have a router.push(....) inside my useEffect, and eslint keeps asking me to add the depenndency router, when I do, I get infinite loop (fetching API constantly) I had to applied the " // eslint-disable-next-line react-hooks/exhaustive-deps" rule in order for the warning to go away... App works fine now... This Es-lint thing, makes things harder sometimes and It makes you beliece you need to re-factor the code... that makes you waste time |
Beta Was this translation helpful? Give feedback.
-
|
A simple thing to do here is to value each use case and then use the proper tool. If I am at the top level const router = useRouter();
useEffect(() => {
// if there's data stay where we are
if(data) return;
// otherwise go to no-data route
router.push("/no-data")
}, [router, data])Then I am in trouble, as I push to However, in this case, because we don't care about the current location, we just care about import Router from 'next/router';
useEffect(() => {
// if there's data stay where we are
if(data) return;
// otherwise go to no-data route
Router.push("/no-data")
}, [data])Now this runs, strictly, only when data changes. Normally, if I have a However, for persistent components, those members of a persistent Layout, the story is different, think of a Header to the entire site, which contains I know this is not optimal, but it is our job as developers to recognise which tool is best for the job at hand. The Router instance is specially important, because you may want to interact with the Router (listen to events for example), outside of React's, at top of modules, or in Class Components.
Another way I've seen people run into runaway effects is with default values: const {data = []} = useDataFetchingTool();
const [, forceRender] = useReducer(x=>!x, false);
useEffect(() => {
forceRender();
}, [data])The above is a made-up example, but when the effect runs and does a |
Beta Was this translation helpful? Give feedback.
-
Adding router to the dependecy array is making some tasks start lagging, why ?I'm trying to sync the URL query param const router = useRouter();
const [section, setSection] = useState<section>("ideas")
useEffect(() => {
router.push({
pathname: '/panel',
query: { section: section },
})
}, [section])The code snippet above runs well and doesn't cause lags, but at build time, I was warned that I have to add the After I added Someone can help me understand what's causing the problem and how I can fix it. |
Beta Was this translation helpful? Give feedback.
-
|
Your component was stuck in a render loop because To prevent it, you should only call export default function Page() {
const { user, loading } = useUser()
const router = useRouter()
useEffect(() => {
if (!(user || loading) && router.pathname !== '/login') { // router.pathname !== '/login' prevents the loop
router.push('/login')
}
}, [user, loading, router]) // router in the dependency array passes ESLint checks
return <p>Redirecting...</p>
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
useRouterdocs show examples whererouteris not part of the dependency array ofuseEffect, e.g.:ESLint clearly complains about this:
Is it recommended to
// eslint-disable-next-line react-hooks/exhaustive-depshere?Does
useRouter()guarantee to return the samerouterinstance every time? If so, I guess it shouldn't hurt addingrouterto the dependency array.I'm wondering why the Next.js docs mentioned above explicitly exclude
routerfrom the dependency array if ESLint complains about this 🤔Beta Was this translation helpful? Give feedback.
All reactions