Router events don't work
Unanswered
Golden northern bumble bee posted this in #help-forum
Golden northern bumble beeOP
In a useEffect inside _app.js I subscribe to router events but none of the are called, including when I navigate to other pages
Any ideas?
const router = useRouter()
useEffect(() => {
router.events.on('routeChangeStart', () => console.log('routeChangeStart'))
router.events.on('routeChangeComplete', () => console.log('routeChangeComplete'))
}, [router.events])Any ideas?
59 Replies
@Ray what is the file path of `_app.js?`
Golden northern bumble beeOP
src/pages/_app.jsWe are still using NextJS 12
But according to the docs, router behaved the same way back then
@Golden northern bumble bee `src/pages/_app.js`
try this
const router = useRouter()
useEffect(() => {
const handleRouteChangeStart = () => console.log('routeChangeStart')
const handleRouteChangeComplete = () => console.log('routeChangeComplete')
router.events.on('routeChangeStart', handleRouteChangeStart)
router.events.on('routeChangeComplete', handleRouteChangeComplete)
return () => {
router.events.off('routeChangeStart', handleRouteChangeStart)
router.events.off('routeChangeComplete', handleRouteChangeComplete)
}
}, [router])@Ray try this
ts
const router = useRouter()
useEffect(() => {
const handleRouteChangeStart = () => console.log('routeChangeStart')
const handleRouteChangeComplete = () => console.log('routeChangeComplete')
router.events.on('routeChangeStart', handleRouteChangeStart)
router.events.on('routeChangeComplete', handleRouteChangeComplete)
return () => {
router.events.off('routeChangeStart', handleRouteChangeStart)
router.events.off('routeChangeComplete', handleRouteChangeComplete)
}
}, [router])
Golden northern bumble beeOP
No, the same. Not a single line in the console. (And I have the Preserve console log option ON)
Wait, did I tell I was switchting to another page?
That's exactly what I was pursuing actually
the terminal one or browser one
Golden northern bumble beeOP
oh
Good point, haha. I was watching only browser log 🙂
it should be printed to browser log
Golden northern bumble beeOP
No, it's definitely not there when I navigate to another page
Also, none of useEffect cleaning functions get called
@Golden northern bumble bee No, it's definitely not there when I navigate to another page
could you show a screenshot of your folder structure?
@Ray could you show a screenshot of your folder structure?
Golden northern bumble beeOP
Here I show from where to where I do the transfer using Link
In the browser console I see:
Navigated to http://localhost:3006/vendors/1/amendments/addSo it seems to be a regular transition
@Golden northern bumble bee So it seems to be a regular transition
do you mean hard navigation?
@Ray do you mean hard navigation?
Golden northern bumble beeOP
Exactly, yes
are you able to navigate to the page you want? because I see the pathname is
'/[id]'?Golden northern bumble beeOP
That's the first id
of the vendor
@Ray are you able to navigate to the page you want? because I see the pathname is `'/[id]'`?
Golden northern bumble beeOP
yes, I do can make the transition
@Golden northern bumble bee yes, I do can make the transition
what is your next version?
Golden northern bumble beeOP
12
12. ?
Golden northern bumble beeOP
NextJS 12, why?
It's a mature project
I want to test on the exact version you have
because It is working for me with next 14
Golden northern bumble beeOP
I see. Sorry, a moment
12.3.4
(We're on our way to upgrade to the latest version, however some things have to be done first, like getting rid of CSS-in-JS)
@Golden northern bumble bee 12.3.4
It is working for me on 12.3.4
Golden northern bumble beeOP
Bah
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 23.3.0: Wed Dec 20 21:31:00 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6020
Binaries:
Node: 20.10.0
npm: 10.2.3
Yarn: N/A
pnpm: 8.15.5
Relevant packages:
next: 12.3.4
eslint-config-next: 14.1.4
react: 18.2.0
react-dom: 18.2.0
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 23.3.0: Wed Dec 20 21:31:00 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6020
Binaries:
Node: 20.10.0
npm: 10.2.3
Yarn: N/A
pnpm: 8.15.5
Relevant packages:
next: 12.3.4
eslint-config-next: 14.1.4
react: 18.2.0
react-dom: 18.2.0
Golden northern bumble beeOP
Could you please share the cli command you used to initialize the test project?
I also want to test on a fresh one
could you create two new page and test?
pnpm create next-app then pnpm add next@12.3.4Golden northern bumble beeOP
so you "create" the latest one and then switch the next version?
yes
because I tried pnpm create next-app@12.3.4 will still install the latest one before
Golden northern bumble beeOP
yes, I encountered that behavior before
@Ray It is working for me on 12.3.4
Golden northern bumble beeOP
I confirm it works, yeah. And also - useEffect disposers are now being called
@Golden northern bumble bee I confirm it works, yeah. And also - useEffect disposers are now being called
yeah so It should be something wrong in your existing one
Golden northern bumble beeOP
Thanks Ray for drawing my attention to this and for making the check for me, that I should have done myself
Golden northern bumble beeOP
I'm gonna copy-paste the main project and then strip it down until I find the difference
@Golden northern bumble bee I'm gonna copy-paste the main project and then strip it down until I find the difference
or are you able to show the full code on _app.js
Golden northern bumble beeOP
Sure
I'm not sure however if it be useful
Maybe react-i18next is in charge... or our getStaticProps
are you able to test without next-i18next?
Golden northern bumble beeOP
I will try