Site Parts in Components in NextJS
In the previous lesson, we wrote the text of the site parts directly in the layout file:
export default function RootLayout({children}) {
return (
<html lang="en">
<body>
<header>
header
</header>
<main>
{children}
</main>
<footer>
footer
</footer>
</body>
</html>
);
}
However, this can be inconvenient when site parts contain a lot of code and text. Therefore, site parts are usually moved into separate component files.
Let's move the site header into a separate component as an example. Let's create the corresponding file:
export default function Header() {
return <>
header
</>;
}
Let's import our component into the layout file:
import Header from '@/comp/header/header.jsx';
export default function RootLayout({children}) {
}
Let's output the text of our component using a tag with its name:
import Header from '@/comp/header/header.jsx';
export default function RootLayout({children}) {
return (
<html lang="en">
<body>
<header>
<Header />
</header>
<main>
{children}
</main>
<footer>
footer
</footer>
</body>
</html>
);
}
Create separate components for the site header and footer.