React Router in React JS
React Router is a popular library for handling client-side routing in React applications. Here's an example of how to use React Router:
jsx
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
function Home() {
return <h1>Home</h1>;
}
function About() {
return <h1>About</h1>;
}
function App() {
return (
<Router>
<div>
<nav>
<ul>
<li>
<
<Route path="/about">
<About />
</Route>
</Switch>
</div>
</Router>
);
}
ReactDOM.render(
<App />,
document.getElementById('root')
);
In this example, the `BrowserRouter`, `Switch`, `Route`, and `Link` components are imported from the `react-router-dom` library. The `App` component uses these components to define the routing for the application.
The `Link` component is used to create links to the different pages of the application. The `Route` component is used to define what component should be rendered based on the current URL.
No comments:
Post a Comment