Moving the <body> tag from Layout to Other Files
Unanswered
Skipjack tuna posted this in #help-forum
Skipjack tunaOP
I have a complex application that intends to have two different rendered views: a Mobile and a Desktop.
For semantic HTML, I'm moving the body tag out of the layout.tsx file, and coupling it with either of these two views.
Is this pattern allowed? The view loads.
src/app/layout.tsx
src/app/page.tsx
src/components/Interface.tsx
For semantic HTML, I'm moving the body tag out of the layout.tsx file, and coupling it with either of these two views.
Is this pattern allowed? The view loads.
src/app/layout.tsx
import "./globals.css";
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return <html lang="en">{children}</html>;
}src/app/page.tsx
// TODO: seperate all client-side rendered logic into their own components
"use client";
import { lazy, Suspense, useEffect, useState } from "react";
// Keep this in sync with the `lg` breakpoint used by the responsive layout.
const breakpoint = "(max-width: 1023px)";
const MobileView = lazy(() =>
import("@/components/Interface").then((module) => ({
default: module.Mobile,
})),
);
const DesktopView = lazy(() =>
import("@/components/Interface").then((module) => ({
default: module.Desktop,
})),
);
export default function Dio() {
const [isMobile, setIsMobile] = useState(false);
useEffect(() => {
const mediaQuery = window.matchMedia(breakpoint);
const updateIsMobile = () => {
setIsMobile(mediaQuery.matches);
};
updateIsMobile();
mediaQuery.addEventListener("change", updateIsMobile);
return () => mediaQuery.removeEventListener("change", updateIsMobile);
}, []);
return (
<Suspense fallback={<div>Loading...</div>}>
{isMobile ? <MobileView /> : <DesktopView />}
</Suspense>
);
}src/components/Interface.tsx
import Viewer from "@/components/Viewer";
export function Mobile() {
return <body>Mobile View</body>;
}
export function Desktop() {
return (
<body>
<menu></menu>
<menu></menu>
<section></section>
<Viewer />
</body>
);
}13 Replies
Serengeti
Is there any purpose of moving body? It sounds bad. I don't know why you can't just use one body tag, it's not about semantic
Serengeti
Some tips from me. You can use
use() api with browser() in suspense from react 19 to make components browser only, and useSyncExternalStore hook can be great for media queriesIt reduces any hydration mismatch
@Skipjack tuna moving body isnt really relevant as it can be just in the layout and dont need to repeat (only when they differ from mobile/desktop). As layout is clientside and the children are then (hopefully you do it like that) serverside, then you wont have hydration mismatches. Rest sounds good for me. When I would be you I would abstract the logic for the breakpoint logic into its own hook like "useMobile" or "useBreakpoint" or whatever feels best for you
@B33fb0n3 <@123623098644692993> moving body isnt really relevant as it can be just in the layout and dont need to repeat (only when they differ from mobile/desktop). As layout is clientside and the children are then (hopefully you do it like that) serverside, then you wont have hydration mismatches. Rest sounds good for me. When I would be you I would abstract the logic for the breakpoint logic into its own hook like "useMobile" or "useBreakpoint" or whatever feels best for you
Serengeti
He doesn't render anything on server now and useSyncExternalStore is suitable here, too
@B33fb0n3 <@123623098644692993> moving body isnt really relevant as it can be just in the layout and dont need to repeat (only when they differ from mobile/desktop). As layout is clientside and the children are then (hopefully you do it like that) serverside, then you wont have hydration mismatches. Rest sounds good for me. When I would be you I would abstract the logic for the breakpoint logic into its own hook like "useMobile" or "useBreakpoint" or whatever feels best for you
Serengeti
Answer why your solution is better or apologize. You leaving dislikes without any understandable reasons, I can also say that you wrote shit this way
@B33fb0n3 I alr explained it in my message
Serengeti
You just said to abstract hook and was wrong about his way, because he doesn't use patterns to keep SSR etc.
I think you don't know what useSyncExternalStore do in addition

I recommended to remove useEffect in react way
@Serengeti You just said to abstract hook and was wrong about his way, because he doesn't use patterns to keep SSR etc.
lets not focus on discussion. Lets focus on finding a solution for OP