We need to create a React app that uses client-side routing to display different pages based on the URL path. The app must always show a navigation bar, handle different routes (like home, fruit details, form, and favorite views), and display a fallback for unknown paths. The goal is to ensure each page has its own dedicated view while sharing global navigation.
File: src/App.jsx
// Pseudocode:
// 1. Wrap the app in <BrowserRouter>
// 2. Always show <Navigation>
// 3. Set up <Routes> with all the necessary paths
// 4. Use * for fallback route
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 fruits={fruits} />} />
<Route path="/fav-fruit" element={<FavoriteFruit fruits={fruits} />} />
<Route path="/set-fav-fruit" element={<SetFavoriteFruit fruits={fruits} />} />
<Route path="*" element={<p>Page Not Found</p>} />
</Routes>
</BrowserRouter>
);
}
export default App;
/<FruitsIndex />/fruits/1<FruitShow /> with fruitId param/randomThink of routing like hallway signs in a building. Each sign points to a different room. In a React app, the BrowserRouter watches the browser address bar and tells React which component to show, based on that path.
We use Route to tell the app: “If someone goes to /, show the home page. If they go to /fruits/2, show the page for fruit #2.”
The <Navigation /> is placed above <Routes /> so it always appears, no matter which page you’re on.
Imagine a museum with a floor plan. Each gallery is a different exhibit. The router is your map, and clicking a link is like walking to a new exhibit. The navigation bar is the info desk that's always visible, helping you get around.
Want to add animations between pages? Check out libraries like Framer Motion or transition groups in React for seamless effects.