React Router Setup and Configuration

Understanding the Problem

When building a React application, we often need to show different components based on the URL path the user visits. Think of it like having different rooms in a house - we need a way to move between them. Our challenge is to:

Planning Our Solution

  1. Set up the basic router structure using createBrowserRouter
  2. Define routes for Home (/), Stocks (/stocks), and Movies (/movies)
  3. Create a catch-all route for handling unknown paths
  4. Connect the router to our React application
  5. Test all routes to ensure proper rendering

Basic Implementation

Let's look at our file structure first:

src/
  ├── App.jsx
  ├── components/
  │   ├── Home/
  │   │   ├── index.js
  │   │   └── Home.jsx
  │   ├── Stocks/
  │   │   ├── index.js
  │   │   └── Stocks.jsx
  │   └── Movies/
  │       ├── index.js
  │       └── Movies.jsx
  └── index.css

Step 1: Basic Router Setup (App.jsx)

// App.jsx
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import Home from './components/Home';
import Stocks from './components/Stocks';
import Movies from './components/Movies';

// Define our routes - think of this as a map of our application
const router = createBrowserRouter([
  {
    // The home page route - like the front door of our house
    path: '/',
    element: <Home />
  },
  {
    // The stocks page - like entering the study
    path: 'stocks',
    element: <Stocks />
  },
  {
    // The movies page - like entering the entertainment room
    path: 'movies',
    element: <Movies />
  },
  {
    // The catch-all route - like having a sign for lost visitors
    path: '*',
    element: <h1>Page Not Found</h1>
  }
]);

function App() {
  return (
    <div className='app'>
      <h1>App Component</h1>
      <RouterProvider router={router} />
    </div>
  );
}

export default App;

Alternative JSX-Based Router Setup

// App.jsx - JSX Version
import { 
  createBrowserRouter, 
  RouterProvider, 
  createRoutesFromElements, 
  Route 
} from 'react-router-dom';

// Creating routes using JSX syntax - more familiar to React developers
const router = createBrowserRouter(
  createRoutesFromElements(
    <Route>
      <Route path="/" element={<Home />} />
      <Route path="stocks" element={<Stocks />} />
      <Route path="movies" element={<Movies />} />
      <Route path="*" element={<h1>Page Not Found</h1>} />
    </Route>
  )
);

Understanding Through Analogies

Think of React Router like a building's navigation system:

Common Challenges and Solutions

Here are some common issues new developers face and how to solve them:

Testing Your Implementation

To verify your router is working correctly, try these steps:

  1. Visit the home page (/) - should see the Home component
  2. Navigate to /stocks - should see the Stocks component
  3. Check /movies - should display the Movies component
  4. Try an invalid path like /potato - should show "Page Not Found"

Real World Applications

This routing pattern is commonly used in:

Next Steps and Practice Ideas

  1. Add nested routes within the Movies component
  2. Create a custom "Page Not Found" component
  3. Add route parameters for dynamic content
  4. Implement loading states for each route

Additional Resources

To deepen your understanding, you might want to explore: