Next.js Discord

Discord Forum

migrate page router to app router.

Answered
Yellowfin tuna posted this in #help-forum
Open in Discord
Yellowfin tunaOP
in next.js 13.4 the app router has been changed to stable. So, I'm trying to migrate my existing page router to the app router.

The _app.jsx is supposed to receive Component, pageProps as props, and I need to get those props in my project now. My question is, when I change this to layout.js, how should I handle the above props?
Answered by Ray
rename index.mdx to page.mdx
View full answer

32 Replies

Yellowfin tunaOP
What should I do now?
what you want to do?
Yellowfin tunaOP
I'm trying to move from Page Router to Router, the project is MDX Blog
create app/layout.tsx first
then you could start migrating the page. page router and app router can use together
Yellowfin tunaOP
Hers is the code on _app file
import { useEffect, useRef } from 'react';

import { Footer } from '@/components/Footer';
import { Header } from '@/components/Header';

import '@/styles/tailwind.css';
import 'focus-visible';

function usePrevious(value) {
  let ref = useRef();

  useEffect(() => {
    ref.current = value;
  }, [value]);

  return ref.current;
}

export default function App({ Component, pageProps, router }) {
  let previousPathname = usePrevious(router.pathname);
  console.log(`hhhhhhhhhh ${pageProps.toString()}`);

  return (
    <>
      <div className="fixed inset-0 flex justify-center sm:px-8">
        <div className="flex w-full max-w-7xl lg:px-8">
          <div className="w-full bg-white ring-1 ring-zinc-100 dark:bg-zinc-900 dark:ring-zinc-300/20" />
        </div>
      </div>
      <div className="relative">
        <Header />
        <main>
          <Component previousPathname={previousPathname} {...pageProps} />
        </main>
        <Footer />
      </div>
    </>
  );
}
i did
basically you don't need those props in layout
Yellowfin tunaOP
But I have a problem with how to write this part in the app router
 <Component previousPathname={previousPathname} {...pageProps} />
what do you use previousPathname for?
Yellowfin tunaOP
To add a back button, but it is not important
so just need to do this in layout.jsx
export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  )
}
Yellowfin tunaOP
this is what I already did
everything is OK but if i click on an article link i will get 404 page
what is the url ?
of the article
same as was on the page router
if you have a time we can make a call
can you show a screenshot of your folder structure?
Yellowfin tunaOP
sure
from app router
rename index.mdx to page.mdx
Answer
Yellowfin tunaOP
Yeeeeeeeees, its work
thank you very much
np
for the usePrevious, you could create a client component and render it in layout or other place
usePathname() hook can get the current path