Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to not render Layout component on NextJS login page

How can exclude render of Layout component when the route is /login, /register etc... in NextJS?

const MyApp = ({ Component, pageProps }) => {
  return (
    <Layout>
      <Component {...pageProps} />
    </Layout>
  );
};

What I have done so far inside Layout.js

const router = useRouter();

if (router.pathname.includes('/login')) return children;
if (router.pathname.includes('/register')) return children;

EDIT: the above solution works, just wanted to know if nextjs routing provides a "good practice" way to solve this problem

like image 472
brand marke Avatar asked Sep 02 '25 17:09

brand marke


1 Answers

Follow these steps to exclude a specific component from layout. This will exclude the dashboard component from the layout.

  1. Add "Dashboard.getLayout" function in the component you want to exclude.
 const Dashboard = ({ children }) => {
return (
  <>
  <DashboardContent children={''}/>
  </>
);
};

export default Dashboard;

Dashboard.getLayout = function PageLayout(page) {
return (
    <>
        {page}
    </>
)
}
  1. Add below code in your _app.js component file.
export default function App({ Component, pageProps }: AppProps) {
if (Component.getLayout) {
return Component.getLayout(
      <Component {...pageProps} />
    )
}
return <Component {...pageProps} />
}
like image 59
Ankit Kumawat Avatar answered Sep 04 '25 05:09

Ankit Kumawat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!