I am trying to get an useEffect to execute some code when a user changes the page via the navigation.
My problem is that the useEffect does not execute on the route change (does not show up in console). It does only show up on the first initial page load and never after
I used the google analytics example from nextJS
This is my simple _app.tsx
import type { AppProps } from "next/app";
import "./styles/css.scss";
import { useRouter } from "next/dist/client/router";
import { useEffect} from "react";
const Child: React.FC = ({ children }) => {
return (
<>
<div>{children}</div>
</>
);
};
const App = ({ Component, pageProps }: AppProps) => {
const router = useRouter();
useEffect(() => {
console.log("called");
}, [router.events]);
return (
<>
<Child>
<Component {...pageProps} />
</Child>
</>
);
};
export default App;