Why I have twice headers and footers in my React app?

This is my very simple React app. I have no idea why it show me 2 copies of header and footer. All components of the page renders in the first <body> once more:

<html lang="en">
    <head></head>
    <body>
    <header>/* header content*/</header>
    <html lang="en">
        <head></head>
            <body>
                <header>/* header content*/</header>
                <h1>Welcome to the main page</h1>
                <footer>/* footer content*/</footer>
            </body>
    </html>**
    <footer>/* footer content*/</footer>
    </body>
</html>

I can’t understand why it happen. This is my code:

app/root.tsx:

import {
    Links,
    Meta,
    Outlet,
    Scripts,
    ScrollRestoration,
} from "@remix-run/react";

import Header from "./routes/components/Header"; 
import Footer from "./routes/components/Footer";

import "./tailwind.css";
import "./style.css";

export function Layout({ children }: { children: React.ReactNode }) {
    return (
        <html lang="en">
        <head>
            <meta charSet="utf-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1" />
            <Meta />
            <Links />
        </head>
        <body>
            <Header />
            {children}
            <Footer />
            <ScrollRestoration />
            <Scripts />
        </body>
    </html>
    );
}

export default function App() {
    return (
        <Layout>
            <Outlet />
        </Layout>
    );
}

app/routes/_index.tsx:

import React from 'react';

const Index = () => {
    return (
        <div className='p-4'>
            <h1>Welcome to the main page</h1>   
        </div>
    );
};

export default Index;

app/routes/components/Header.tsx:

import React from 'react';

const Header: React.FC = () => {
    return (
        <header>
           <div className="p-4">
              <h1 className="text-3xl">My magic app</h1>
            </div>
        </header>
    );
};

export default Header;

app/routes/components/Footer.tsx:

import React from 'react';

const Footer: React.FC = () => {
    return (
        <footer>
            <p className="py-1 text-base sm:text-sm">© 2024, MadFox. All rights reserved.</p>
        </footer>
    );
};
export default Footer;

What is the problem?

  • When I deleted <Header /> from the function Layout in the app/root.tsx I see only two footers, I don’t see <h1 className="text-3xl">My magic app</h1>.
  • When I deleted <Footer /> from the function Layout in the app/root.tsx I see only two headers, I don’t see <h1 className="text-3xl">My magic app</h1>.
  • When I deleted both <Header /> and <Footer /> from the function Layout in the app/root.tsx I see nothing on the page, internal <body> is empty.

0

First of all, the html code is strange.

Please start by writing why html was used in duplicate.

1

Currently, your Layout component is rendering a complete html-document (<html>, <head>, and <body> tags).

Usually, React will take care of these tags.

Instead, you should wrap it in a fragment (<> </>) and avoid rendering the <html>, <head>, and <body> tags:

app/root.tsx:

import {
    Links,
    Meta,
    Outlet,
    Scripts,
    ScrollRestoration,
} from "@remix-run/react";

import Header from "./routes/components/Header"; 
import Footer from "./routes/components/Footer";

import "./tailwind.css";
import "./style.css";

export function Layout({ children }: { children: React.ReactNode }) {
    return (
        <>
            <Header />
            {children}
            <Footer />
            <ScrollRestoration />
            <Scripts />
        </>
    );
}

export default function App() {
    return (
        <Layout>
            <Outlet />
        </Layout>
    );
}

I resolve the problem! So, there is no need to add and in the root.tsx. They will be place in the index page:

root.tsx:

export function Layout({ children }: { children: React.ReactNode }) {
    return (
        <html lang="en">
            <head>
            <meta charSet="utf-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1" />
            <Meta />
            <Links />
            </head>
            <body>
                {children}
                <ScrollRestoration />
                <Scripts />
            </body>
        </html>
    );
}

export default function App() {
  return (
    <Layout>
      <Outlet />
    </Layout>
  );
}

_index.tsx:

import React from 'react';
import ReactDOM from 'react-dom';
import Header from "../components/Header"; 
import Footer from "../components/Footer";
import Bridge from '../components/Bridge';


const Index = () => {
    return (
        <div className='wrap'>
            <Header />
            <Footer />
        </div> 
    );
};

export default Index;

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật