Building a Simple Product Listing Server Component in React
Server Components in React offer a powerful way to fetch data and render UI directly on the server, improving performance and SEO. This challenge asks you to build a basic server component that fetches a list of products from a mock API and displays them. This exercise will solidify your understanding of Server Components, data fetching, and rendering in a React application.
Problem Description
You need to create a React Server Component called ProductList that fetches a list of products from a mock API endpoint (/api/products) and renders them as a simple list. The component should handle potential errors during data fetching and display an appropriate message if the fetch fails. The component should render a loading state while the data is being fetched. Each product should display its ID and name.
Key Requirements:
- Server Component: The component must be a Server Component (using
'use server'). - Data Fetching: Fetch data from the
/api/productsendpoint. Assume this endpoint returns a JSON array of product objects. Each product object has anid(number) and aname(string) property. - Loading State: Display a loading indicator while the data is being fetched.
- Error Handling: Display an error message if the data fetching fails.
- Rendering: Render the fetched product data as an unordered list (
<ul>) with list items (<li>) displaying the product ID and name. - No Client-Side Interaction: This component should not have any client-side interactivity (e.g., buttons, forms).
Expected Behavior:
- Upon initial render, the component should display a loading indicator.
- After the data is fetched successfully, the loading indicator should disappear, and the list of products should be displayed.
- If the data fetching fails (e.g., network error, API returns an error), the loading indicator should disappear, and an error message should be displayed.
Examples
Example 1:
Input: Mock API returns: `[{id: 1, name: "Laptop"}, {id: 2, name: "Mouse"}, {id: 3, name: "Keyboard"}]`
Output:
```html
<ul>
<li>Product ID: 1, Name: Laptop</li>
<li>Product ID: 2, Name: Mouse</li>
<li>Product ID: 3, Name: Keyboard</li>
</ul>
Explanation: The component successfully fetches the product data and renders it as a list.
Example 2:
Input: Mock API returns an error (e.g., 500 Internal Server Error)
Output:
```html
<p>Error fetching products. Please try again later.</p>
Explanation: The component encounters an error during data fetching and displays an error message.
Example 3:
Input: Mock API returns: `[]` (empty array)
Output:
```html
<p>No products found.</p>
Explanation: The component fetches an empty array, indicating no products are available.
Constraints
- Data Format: The API endpoint
/api/productsis expected to return a JSON array of objects withid(number) andname(string) properties. - Error Handling: Assume the API might return a non-200 status code, which should be treated as an error.
- Performance: The component should fetch data efficiently and render the UI quickly. While not a strict constraint, avoid unnecessary re-renders.
- No Client-Side Code: The component must be a Server Component. Do not include any client-side code or dependencies.
Notes
- You'll need to create a mock API route
/api/productsto simulate the data fetching process. This route should return a JSON array of product objects, or simulate an error. You can usefetchwithin your API route. - Consider using
try...catchblocks to handle potential errors during data fetching. - Use React's built-in loading state management (e.g., a
loadingboolean variable) to control the display of the loading indicator. - Think about how to display a user-friendly message when no products are found.
- Focus on the core functionality of fetching data and rendering it in a Server Component. Styling is not required for this challenge.
- Remember to use
'use server'at the top of your component.