Phase 3: FruitsIndex Component Implementation

Understanding the Problem

In this React practice assessment, we need to implement a FruitsIndex component that displays a list of fruits from mock data. According to the test file, the component should:

  1. Render an h2 heading with the text "Fruits Index"
  2. Display a list of fruits from the mock data
  3. Each fruit should be a link to a detail page with the URL /fruits/:id
  4. The component should be rendered at the root route "/"

We've been provided with:

Devising a Plan

  1. Update the FruitsIndex component to accept fruits as a prop
  2. Import the Link component from react-router-dom
  3. Render an h2 heading with "Fruits Index"
  4. Map through the fruits array to create a list of links
  5. Add error handling for when fruits is undefined or not an array
  6. Update the App.jsx file to pass the fruits data to FruitsIndex

Implementing the Solution

Step 1: Understand the Current Code

First, let's look at the current FruitsIndex.jsx file:

function FruitsIndex() {
  return null;
}

export default FruitsIndex;

And the relevant part of App.jsx:

function App() {
  return (
    <BrowserRouter>
      <Navigation />
      <Routes>
        <Route path="/" element={} />
        <Route path="/fruits/:fruitId" element={} />
        <Route path="/fruits/new" element={} />
        <Route path="/fav-fruit" element={} />
        <Route path="/set-fav-fruit" element={} />
        <Route path="*" element={<p>Page Not Found</p>} />
      </Routes>
    </BrowserRouter>
  );
}

Step 2: Update FruitsIndex.jsx

We need to modify FruitsIndex to:

// FruitsIndex.jsx
import React from 'react';
import { Link } from 'react-router-dom';

function FruitsIndex({ fruits }) {
  // Check if fruits prop exists and is an array
  if (!fruits || !Array.isArray(fruits)) {
    return <h2>Fruits Index</h2>; // Still render the heading even if no fruits
  }

  return (
    <div>
      <h2>Fruits Index</h2>
      <ul>
        {fruits.map(fruit => (
          <li key={fruit.id}>
            <Link to={`/fruits/${fruit.id}`}>{fruit.name}</Link>
          </li>
        ))}
      </ul>
    </div>
  );
}

export default FruitsIndex;

Step 3: Update App.jsx

Now, we need to update App.jsx to use the FruitsIndex component and pass the fruits data:

// App.jsx
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import Navigation from './Navigation';
import FruitsIndex from './FruitsIndex';
import FruitShow from './FruitShow';
import FruitForm from './FruitForm';
import FavoriteFruit from './FavoriteFruit';
import SetFavoriteFruit from './SetFavoriteFruit';
import fruits from './mockData/fruits.json';

function App() {
  return (
    <BrowserRouter>
      <Navigation />
      <Routes>
        <Route path="/" element={<FruitsIndex fruits={fruits} />} />
        <Route path="/fruits/:fruitId" element={<FruitShow fruits={fruits} />} />
        <Route path="/fruits/new" element={<FruitForm />} />
        <Route path="/fav-fruit" element={<FavoriteFruit />} />
        <Route path="/set-fav-fruit" element={<SetFavoriteFruit />} />
        <Route path="*" element={<p>Page Not Found</p>} />
      </Routes>
    </BrowserRouter>
  );
}

export default App;

Code Explanation

FruitsIndex Component

App Component

Looking Back

Let's verify that our solution meets all the requirements:

To test this implementation, run:

npm test

Real-World Application

This index-to-detail pattern is extremely common in web applications:

For example, when you shop on Amazon, you see a list of products (index) and can click on any to see more details (show page).

Additional Considerations