Skip to content

Latest commit

 

History

History
54 lines (42 loc) · 2.15 KB

s4 react08.md

File metadata and controls

54 lines (42 loc) · 2.15 KB

Introduction to React.js: Lesson 8 - React Router

Welcome to the eighth lesson of the "Introduction to React.js" course! In this lesson, we'll explore React Router, a popular library for adding navigation and routing functionality to your React applications.

Understanding Routing in Single-Page Apps In a single-page application (SPA), the content is dynamically updated without requiring full page reloads. React Router helps you achieve this by providing components to manage different views and navigation.

Setting Up React Router To use React Router, you need to install the react-router-dom package:

npm install react-router-dom

Creating Routes In React Router, you define routes using the Route component. Each route maps a specific URL path to a corresponding component.

import React from 'react';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
import Home from './Home';
import About from './About';

function App() {
  return (
    <Router>
      <nav>
        <ul>
          <li>
            <Link to="/">Home</Link>
          </li>
          <li>
            <Link to="/about">About</Link>
          </li>
        </ul>
      </nav>

      <Route path="/" exact component={Home} />
      <Route path="/about" component={About} />
    </Router>
  );
}

export default App;

Navigating Between Routes React Router provides the Link component to create navigation links that update the URL and render the appropriate route.

Your Task: Create a Multi-Page App For this lesson's assignment, create a multi-page app using React Router. Create at least two different components representing different pages. Set up navigation using the Link component to switch between these pages.

Conclusion Congratulations! You've completed the eighth lesson in the "Introduction to React.js" course. You've learned how to use React Router to add navigation and routing functionality to your React applications. Routing is crucial for creating multi-page-like experiences within a single-page app. In the next lesson, we'll dive into state management with Redux. Keep up the great work on your journey to mastering React.js!