What Are Components?
-
Components are the building blocks of React apps.
-
They are reusable pieces of UI, usually defined as functions or classes.
-
Components help keep your code modular and manageable.
What Are Layouts?
-
Layouts are special components used to define common UI structures (like headers, footers, navigation) shared across multiple pages.
-
They help avoid repetition by wrapping page content with consistent UI elements.
How to Use Components in Next.js
Create a reusable component:
Use it in a page:
Creating a Layout Component
Example of a basic layout wrapping children content:
Use the layout in a page:
Applying Layout Globally Using _app.js
To avoid importing layout in every page, wrap all pages globally:
Now, every page automatically uses the Layout.
Summary
| Concept | Purpose |
|---|---|
| Component | Reusable UI pieces for buttons, forms, etc. |
| Layout | Shared page structure (header, footer, nav) |
_app.js |
Wrap pages globally with layouts or providers |
