React and Next.js are related, but they serve different roles.
| Feature | React | Next.js |
|---|---|---|
| Main role | UI library | React framework |
| Components | Yes | Yes, using React |
| Routing | Usually add a router or use another solution | Built-in App Router and Pages Router |
| Server rendering | Possible with additional architecture and tools | Integrated rendering conventions |
| File-based routes | Not a core React feature | Yes |
| Backend endpoints | Not included in React itself | Route Handlers and other server capabilities |
| Data access | Depends on the application architecture | Server-side data access can be integrated |
| Image/font optimization | Choose tools separately | Framework integrations available |
| Deployment | Many options | Multiple options, with platform-specific features depending on deployment |
With a typical client-rendered React application, you might need to configure:
Next.js supplies conventions for these concerns, allowing you to focus more on your application’s features.
tsx
// app/products/page.tsx
export default async function ProductsPage() {
const response = await fetch(
"https://api.example.com/products"
);
if (!response.ok) {
throw new Error("Failed to load products");
}
const products = await response.json();
return (
<main>
<h1>Products</h1>
<ul>
{products.map(
(product: { id: string; name: string }) => (
<li key={product.id}>{product.name}</li>
)
)}
</ul>
</main>
);
}This illustrates fetching data in a Server Component. In a real project, you would also define the response type, decide on caching and revalidation behavior, and handle loading and failure states.
Note that the example URL is a placeholder, not a working API.
Important: You can use React without Next.js. A Next.js application uses React.