React Router
React Router is a popular library for implementing client-side routing in a React application. It allows you to define different routes in your application and render different components based on the current URL.
Installation
To get started with React Router, you need to install it as a dependency in your project. You can install it via npm or yarn:
SNIPPET
1npm install react-router-dom
xxxxxxxxxx
27
// Example code for client-side routing in React using React Router
import React from 'react';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import Home from './components/Home';
import About from './components/About';
import Contact from './components/Contact';
function App() {
return (
<Router>
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route path="/about">
<About />
</Route>
<Route path="/contact">
<Contact />
</Route>
</Switch>
</Router>
);
}
export default App;
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment