Next.js Discord

Discord Forum

layout.jsx Conditional rendering

Answered
Bordin posted this in #help-forum
Open in Discord
Hello, I am trying to make a layout.tsx render parallel routes based on the URL
meaning if i was in dashboard/AddItem i wanna render only the add item form in the dashboard.
"use client";
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import { useRouter } from "next/router";
const inter = Inter({ subsets: ["latin"] });

export default function RootLayout({
  children,
  AddCategory,
}: Readonly<{
  children: React.ReactNode;
  AddCategory: React.ReactNode;
}>) {
  const router = useRouter();
  const { pathname } = router;
  const isAddCategoryPage = pathname.endsWith("/addCategory");
  return (
    <html lang="en">
      <body className={inter.className} style={{ display: "flex" }}>
        <div>{children}</div>
        {isAddCategoryPage ? <div>{AddCategory}</div> : ""}
      </body>
    </html>
  );
}


However i am getting an error
Error: NextRouter was not mounted.

the error makes sense, but idk how to overcome it and im not sure if what i am doing is even good practice
Answered by Ray
then create the page at Dashboard/@AddCategory/AddCategory/page.tsx
View full answer

95 Replies

  const router = useRouter();
  const isAddCategoryPage = router.usePathname();
usePathname doesnt exist
@Bordin usePathname doesnt exist
no
import { usePathname } from 'next/navigation'
ah i see
so i this good practice?
@Bordin so i this good practice?
I don't think it is good practice by marking the root layout because you can't use the metadata api with client component
http://localhost:3000/Dashboard/AddCategory
Ok it is working but i am getting page not found when going to this route.
which is understandable considering there is no page with such route.
but i was hopeing fot the layout to render and then render the form addcategory in the dashboard
When i press "Add Category" Link, it takes me to /Dashboard/AddCategory
and i wanna add the addcategory form in the white space
without removing the dashboard
should i change the dashboard page to template?
@Bordin without removing the dashboard
import type { Metadata } from "next";
import { Inter } from "next/font/google";
const inter = Inter({ subsets: ["latin"] });

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="en">
      <body className={inter.className} style={{ display: "flex" }}>
        <div>{children}</div>
      </body>
    </html>
  );
}
and rename Dashboard/@AddCategory/page.tsx to Dashboard/AddCategory/page.tsx
the dashboard gets removed
when i go addcategory
it needs to stay
where is dashboard?
/Dashboard/page.tsx
oh ok
yes
import type { Metadata } from "next";
import { Inter } from "next/font/google";
const inter = Inter({ subsets: ["latin"] });
import Dashboard from "./page";
export default function RootLayout({
  children,
  Dashboard,
}: Readonly<{
  children: React.ReactNode;
  Dashboard: React.ReactNode;
}>) {
  return (
    <html lang="en">
      <body className={inter.className} style={{ display: "flex" }}>
        <div>{Dashboard}</div>
        <div>{children}</div>
      </body>
    </html>
  );
}

i tried doing this but it still doesnst work
then create the page at Dashboard/@AddCategory/AddCategory/page.tsx
Answer
why?
@Bordin why?
not working
@Bordin not working
could you show a screenshot of your folder structure
nvm i fixed it
is that what you want?
nope, its not working on reload
i tried that
it didnt work
try again
and show the error here
addcategory page isnt rendering
and when i reload
it returns 404
show the Dashboard/layout.tsx
import type { Metadata } from "next";
import { Inter } from "next/font/google";
const inter = Inter({ subsets: ["latin"] });
export default function RootLayout({
  children,
  Dashboard,
}: Readonly<{
  children: React.ReactNode;
  AddCategory: React.ReactNode;
  Dashboard: React.ReactNode;
}>) {
  return (
    <>
      {Dashboard}
      <div>{children}</div>
    </>
  );
}
`
@Bordin tsx import type { Metadata } from "next"; import { Inter } from "next/font/google"; const inter = Inter({ subsets: ["latin"] }); export default function RootLayout({ children, Dashboard, }: Readonly<{ children: React.ReactNode; AddCategory: React.ReactNode; Dashboard: React.ReactNode; }>) { return ( <> {Dashboard} <div>{children}</div> </> ); } `
ok change this
import type { Metadata } from "next";
import { Inter } from "next/font/google";
const inter = Inter({ subsets: ["latin"] });
export default function RootLayout({
  children,
  AddCategory,
}: Readonly<{
  children: React.ReactNode;
  AddCategory: React.ReactNode;
}>) {
  return (
    <>
      {AddCategory}
      <div>{children}</div>
    </>
  );
}
the slot is @AddCategory now
404
for both dashboard and dashboard/addcategory
@Bordin for both dashboard and dashboard/addcategory
add this
// Dashboard/@AddCategory/default.tsx
export default function Default() {
  return null
}
Dashboard/@AddCategory/default.tsx
okay
it works but 404 on reload
@Bordin it works but 404 on reload
which url were you at?
/dashboard/additem
you dont have additem route?
i mean addcategory
mb
@Bordin i mean addcategory
ok add this
// Dashboard/default.tsx
export { default } from './page'
still..
wait
thats weird
@Bordin thats weird
you mean the styles are gone?
ye
where do you import the style?
scss
yea where are you importing it?
import "../scss/DashboardPage.scss";
in dashboard/page.tsx
you should import on layout
Dashboard/layout.tsx
eyy it works
thank you Ray!
@Bordin thank you Ray!
np
but
can you explain whats happening plz
about the style?
why did we need two @Addcategory and AddCategory folders
oh
why did we need default.tsx inside @AddCategory not inside AddCategory?
read the doc, too much detail :lolsob:
why did we need page.tsx inside AddCategory not inside @AddCategory x.x
Okay fairwell, thank you for your help and time
idk how to add solved to the post
there isnt an "Apps" option when i rightclick
I'll do it