By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – In Next JS, changes in layout.tsx only applies to home page, other pages are not affected?
JavaScript

Javascript – In Next JS, changes in layout.tsx only applies to home page, other pages are not affected?

Admin
Last updated: 2024/01/07 at 7:17 AM
Admin
Share
3 Min Read
In Next JS, changes in layout.tsx only applies to home page, other pages are not affected?

Problem:

UPDATE

Contents
Problem:Solution:

My answer is not working: Next JS 13 does not recommend adding _app.tsx or _document.tsx, instead, we should use the default layout.tsx.

With my solution, Navbar and Footer are added to all pages but there is a console error:
Uncaught Error: invariant expected app router to be mounted
which requires the html and body tags in layout.tsx. However, if I change layout.tsx back to it’s default, the previously added _document.tsx adds an extra html tag to the page HTML, causing the following error:
Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Expected server HTML to contain a matching <html> in <div>.

I’m still looking for a solution.

I have a Next JS app, the version is 13.5.4, its structured like this:

- pages
-- about.tsx
- src
-- app
--- layout.tsx
--- page.tsx

In order to not repeat code, I changed the default layout.tsx to this:

import './globals.scss'
import type { Metadata } from 'next'
import { Inter } from 'next/font/google'
import {Navbar} from "@/app/components/Navbar/navbar";
import {Footer} from "@/app/components/Footer/footer";

const inter = Inter({ subsets: ['latin'] })

export const metadata: Metadata = {
  title: 'Create Next App',
  description: 'Generated by create next app',
}

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html lang="en">
      <body className={inter.className}>
        <Navbar /> // ---- added this
          {children}
        <Footer /> // ---- added this
      </body>
    </html>
  )
}

My goal is to have Navbar and Footer on every page by default. Now the problem is, they are on homepage, i.e. applied to page.tsx. But they are not on /about. Any idea why?

Here is the navbar.tsx which routes to the /about.

import Link from "next/link";
export const Navbar = () => {
  return (
    <nav>
      <div>
        <ul>
          <li>
            <Link href="/#">Home</Link>
          </li>
          <li>
            <Link href="/about">About</Link>
          </li>
        </ul>
      </div>
    </nav>
  )
}

Solution:

I have found another solution:

The pages folder should be empty, everything should be in /src/app The correct project layout should be:

|-project
|--src
|---app
|----about
|-----page.tsx
|----components
|-----Navbar
|------Navbar.tsx
|-----Footer
|------Footer.tsx
|----globals.scss
|----layout.tsx
|----page.tsx

The layout.tsx should be:

import { Navbar } from "@/app/components/Navbar/navbar";
import { Footer } from "@/app/components/Footer/footer";
import './globals.scss'

interface LayoutProps {
  children: React.ReactNode;
}
export default function RootLayout({ children }: LayoutProps) {
  return (
    <html lang="en">
      <body>
        <main className="flex min-h-screen flex-col items-center justify-between p-24 main">
          <Navbar />
          {children}
          <Footer />
        </main>
      </body>
    </html>
  )
}  

Then src/app/about/page.tsx should be

export default function About() {
  return (
    <div>
      About me
    </div>
  )
}

Then there is no console error, the navbar and footer are on every page without defining in each page .tsx file.

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Need help optimizing a loop of merging objects and arrays Javascript – Need help optimizing a loop of merging objects and arrays
Next Article Pass data via CustomEvent from JS to Java Javascript – Pass data via CustomEvent from JS to Java
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?