How do you implement routing in a React application

0 votes
With the help of code can you tell me How do you implement routing in a React application?
Feb 22 in Node-js by Ashutosh
• 20,870 points
68 views

1 answer to this question.

0 votes

Implementing Routing in a React Application (React Router)

1. Install React Router

npm install react-router-dom

2. Set Up Routes in App.js

import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

import Home from "./Home";

import About from "./About";

const App = () => {

  return (

    <Router>

      <Routes>

        <Route path="/" element={<Home />} />

        <Route path="/about" element={<About />} />

      </Routes>

    </Router>

  );

};

export default App;

3. Create Navigation Links

import { Link } from "react-router-dom";

const Navbar = () => {

  return (

    <nav>

      <Link to="/">Home</Link>

      <Link to="/about">About</Link>

    </nav>

  );

};

export default Navbar;

4. Use in Components

import { useNavigate } from "react-router-dom";

const Home = () => {

  const navigate = useNavigate();

  return <button onClick={() => navigate("/about")}>Go to About</button>;

};

export default Home;

answered Feb 23 by Kavya

Related Questions In Node-js

0 votes
1 answer

How do you implement breadcrumbs in a React-Router app?

Breadcrumbs help users navigate by showing the ...READ MORE

answered Feb 24 in Node-js by Kavya
70 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do you handle a large amount of data in React?

Handling Large Amounts of Data in React To ...READ MORE

answered Feb 24 in Node-js by Kavya
68 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to set read concern in MongoDB?

In MongoDB, you can set read concern ...READ MORE

answered Feb 23 in Node-js by Kavya
59 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP