How to use of Router in React||React Router
Tags: webdev, javascript, beginners, programming
Published: 2024-03-17 17:51:05

In React, routing is typically handled using third-party libraries like React Router. React Router is a popular routing library for React that enables you to create dynamic, single-page applications with multiple views or pages. Here's a basic example of how you can set up routing in a React application using React Router:

1. First, you need to install React Router in your project. You can do this via npm or yarn:

```
npm install react-router-dom

```
or

```
yarn add react-router-dom

```
2. Once React Router is installed, you can set up routing in your application. Here's an example of how you might structure your `App.js` file:

```js
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Home from './components/Home';
import About from './components/About';
import Contact from './components/Contact';
import NotFound from './components/NotFound';

function App() {
  return (
    <Router>
      <div>
        <Switch>
          <Route exact path="/" component={Home} />
          <Route path="/about" component={About} />
          <Route path="/contact" component={Contact} />
          <Route component={NotFound} />
        </Switch>
      </div>
    </Router>
  );
}

export default App;

```
1. Create the components for your different pages. For example, you might have a `Home.js`, `About.js`, `Contact.js`, and `NotFound.js` file in a components directory:

```js
// Home.js
import React from 'react';

function Home() {
  return (
    <div>
      <h2>Home</h2>
      <p>Welcome to our website!</p>
    </div>
  );
}

export default Home;

```

```js
// About.js
import React from 'react';

function About() {
  return (
    <div>
      <h2>About Us</h2>
      <p>Learn more about our company.</p>
    </div>
  );
}

export default About;

```

```js
// Contact.js
import React from 'react';

function Contact() {
  return (
    <div>
      <h2>Contact Us</h2>
      <p>Get in touch with us.</p>
    </div>
  );
}

export default Contact;


```

```js
// NotFound.js
import React from 'react';

function NotFound() {
  return (
    <div>
      <h2>404 Not Found</h2>
      <p>Oops! Page not found.</p>
    </div>
  );
}

export default NotFound;


```
1. Finally, you can start your application and navigate between different routes. Each time you navigate to a different route, the corresponding component will be rendered.
This is a basic example of how you can implement routing in a React application using React Router. React Router offers many more features and options for configuring your routes, such as nested routes, route parameters, and more. Be sure to check out the React Router documentation for more information and advanced usage.

[github](https://github.com/04anilr)
[website](https://my-portfolio-two-lemon-36.vercel.app/)


[Array methods in react.js ](https://dev.to/04anilr/how-to-use-array-methods-in-real-project-example-add-new-record-3o82)
[Fragment in react.js](https://dev.to/04anilr/fragment-in-reactjs-example-of-fragment-299a) 
[Conditional rendering in react.js](https://dev.to/04anilr/conditional-rendering-in-react-conditional-rendering-example-in-react-3j46)
[Children component in react.js](https://dev.to/04anilr/methods-of-react-children-component-in-react-4ol7)
[use of Async/Await in react.js](https://dev.to/04anilr/how-to-use-asyncawait-3a3o)
[Array methods in react.js](https://dev.to/04anilr/array-methods-in-reactjs-36jb)
[JSX in react.js](https://dev.to/04anilr/jsx-in-reactjs-3f7o)
[Event Handling in react.js](https://dev.to/04anilr/event-handling-in-react-5hc7)
[Arrow function in react.js](https://dev.to/04anilr/arrow-functions-in-react-4gb2)
[Virtual DOM in react.js](https://dev.to/04anilr/how-to-work-virtual-dom-in-react-dom-1b39)
[React map() in react.js](https://dev.to/04anilr/how-to-create-react-map-24pp)
[How to create dark mode in react.js](https://dev.to/04anilr/how-to-create-dark-mode-in-react-js-2kn2)
[How to use of Props in react.js](https://dev.to/04anilr/props-and-state-with-example-1bl1)
[Class component and Functional component](https://dev.to/04anilr/class-component-functional-component-in-react-k43)
[How to use of Router in react.js](https://dev.to/04anilr/how-to-use-of-router-in-reactreact-router-1163)
[All React hooks explain ](https://dev.to/04anilr/react-hooks-cheat-sheet-2hic)
[CSS box model ](https://dev.to/04anilr/css-box-model-work-2k0a)
[How to make portfolio nav-bar in react.js](https://dev.to/04anilr/how-to-make-nav-bar-for-portfolio-website-365m)