Course Content
React JSX
React JSX (JavaScript XML) is a syntax extension for JavaScript that allows you to write HTML-like code within JavaScript. JSX makes it easier to create React components and define their structure in a more readable and declarative way.
0/1
Components in React
In React.js, components are the building blocks of a React application. They allow you to break the UI into independent, reusable pieces and think about each piece in isolation. Here's an overview of components in React:
0/1
Props in React
Props (short for properties) are a way to pass data from a parent component to a child component in React. They are read-only and cannot be modified by the child component, ensuring a unidirectional data flow.
0/1
React Events
Just like HTML DOM events, React can perform actions based on user events.React has the same events as HTML: click, change, mouseover etc.
0/1
React Conditional Rendering
0/1
React Lists
0/1
React Forms
0/1
Styling React Using CSS
0/1
React Router
0/1
React Js
About Lesson

React Router:

React Router is used to handle navigation in React applications. It allows switching between pages (routes) without reloading the page.

Run the following command in your project:

     npm install react-router-dom

Example:

JavaScript
import React from "react";
import { BrowserRouter, Routes, Route, Link } from "react-router-dom";
import Home from "./Home";
import About from "./About";
import Contact from "./Contact";
function App() {
  return (
    < BrowserRouter >
      <nav>
        <Link to="/">Home</Link> | 
        <Link to="/about">About</Link> | 
        <Link to="/contact">Contact</Link>
      </nav>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
        <Route path="/contact" element={<Contact />} />
      </Routes>
    </ BrowserRouter >
  );
}

export default App;
JavaScript
import { useNavigate } from "react-router-dom";
function Home() {
  const navigate = useNavigate();

  const goToContact = () => {
    navigate("/contact");
  };
  return (
    <div>
      <h2>Home Page</h2>
      <button onClick={goToContact}>Go to Contact</button>
    </div>
  );
}
export default Home;

You can pass parameters in the URL like /profile/:id.

App.js

<Route path="/profile/:id" element={<Profile />} />

Profile.js

JavaScript
import { useParams } from "react-router-dom";
function Profile() {
  const { id } = useParams();
  return <h2>Profile Page - User ID: {id}</h2>;
}
export default Profile;

If a user tries to access a non-existing page, you can redirect them.

Example:

JavaScript
import { Navigate } from "react-router-dom";

function NotFound() {
  return <Navigate to="/" />;
}

If you want to restrict access to a route, use a Protected Route.

ProtectedRoute.js:

JavaScript
import { Navigate } from "react-router-dom";
function ProtectedRoute({ children }) {
  const isAuthenticated = false; // Change this based on auth state
  return isAuthenticated ? children : <Navigate to="/" />;
}
export default ProtectedRoute;

Using ProtectedRoute:

JavaScript
<Route path="/dashboard" element={<ProtectedRoute><Dashboard /></ProtectedRoute>} />