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:
- Render an h2 heading with the text "Fruits Index"
- Display a list of fruits from the mock data
- Each fruit should be a link to a detail page with the URL
/fruits/:id - The component should be rendered at the root route "/"
We've been provided with:
- A starter FruitsIndex.jsx file that returns null
- An App.jsx file with routes set up but missing elements
- A fruits.json file with mock data of four fruits
Devising a Plan
- Update the FruitsIndex component to accept fruits as a prop
- Import the Link component from react-router-dom
- Render an h2 heading with "Fruits Index"
- Map through the fruits array to create a list of links
- Add error handling for when fruits is undefined or not an array
- 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:
- Accept fruits as a prop
- Render an h2 with "Fruits Index"
- Map through the fruits to create links
// 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
- Imports: We import React and Link from react-router-dom
- Props Destructuring: We use
{ fruits }to extract the fruits prop - Error Handling: We check if fruits exists and is an array
- JSX Structure: We render an h2 and a list of links
- Map Function: We use
fruits.map()to create a list item for each fruit - Key Prop: We set a unique key for each list item using the fruit's id
- Link Component: We use Link to create links to individual fruit pages
App Component
- Route Setup: We set up the routes for our app
- Props Passing: We pass the fruits data to the FruitsIndex component as a prop
Looking Back
Let's verify that our solution meets all the requirements:
- ✅ FruitsIndex renders an h2 with "Fruits Index"
- ✅ It displays a list of all fruits from the mock data
- ✅ Each fruit is a link to
/fruits/:id - ✅ The component is rendered at the root route
To test this implementation, run:
npm test
Real-World Application
This index-to-detail pattern is extremely common in web applications:
- E-commerce: Product listings that link to product details
- Content Management: Article lists that link to full articles
- Social Media: User feeds that link to individual posts
- Data Dashboards: Summary views that link to detailed analysis
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
- Styling: Add CSS to make the list more visually appealing
- Filtering: Add search or filter functionality for longer lists
- Sorting: Allow users to sort the fruits by different criteria
- Pagination: For larger datasets, implement pagination
- Loading States: Add loading indicators for data fetched from APIs
- Error Boundaries: Implement more robust error handling