27/07/2024
Next.js has continually evolved to meet the demands of modern web applications, and with the release of Next.js 14, it brings forth a powerful distinction between server and client components. Understanding these two types of components is essential for developers looking to build efficient and performant applications.
Server Components are React components that are rendered on the server side. This means that when a user requests a page, the server does all the necessary computations, rendering, and data fetching, sending the fully formed HTML to the client. As a result, server components are aimed at optimizing performance by reducing the amount of JavaScript sent to the client.
// app/components/UserList.server.jsx import React from 'react'; import fetchUsers from '../lib/fetchUsers'; const UserList = async () => { const users = await fetchUsers(); return ( <ul> {users.map((user) => ( <li key={user.id}>{user.name}</li> ))} </ul> ); }; export default UserList;
In this example, the UserList
component fetches user data on the server side. When a user navigates to the page containing UserList
, they immediately receive a rendered list of users without waiting for JavaScript to execute on the client.
Client Components, on the other hand, are components that are rendered on the client side. These components require JavaScript to execute in the user's browser and can manage their own state, handle user events, and perform side effects.
useState
and useEffect
) to manage local component state, making them ideal for dynamic UI updates.// app/components/UserForm.client.jsx 'use client'; import React, { useState } from 'react'; const UserForm = () => { const [name, setName] = useState(''); const handleSubmit = (e) => { e.preventDefault(); alert(`Submitted Name: ${name}`); }; return ( <form onSubmit={handleSubmit}> <input type="text" value={name} onChange={(e) => setName(e.target.value)} placeholder="Enter your name" /> <button type="submit">Submit</button> </form> ); }; export default UserForm;
Here, UserForm
is a client component that manages a local state for the user's name. It interacts with the user and handles the form submission directly in the browser.
When building an application with Next.js 14, the choice between server and client components should be dictated by the functional requirements of your application. Here are some considerations:
By taking advantage of both component types, you can tailor your application’s architecture to optimize performance while delivering a rich user experience.
08/09/2024 | Next.js
02/10/2024 | Next.js
02/10/2024 | Next.js
27/07/2024 | Next.js
08/09/2024 | Next.js
08/09/2024 | Next.js
08/09/2024 | Next.js
02/10/2024 | Next.js
30/11/2024 | Next.js