React Router not only allows you to define routes and render components, but it also enables seamless navigation between pages. Let's explore the world of navigation in React Router and learn how to efficiently handle page transitions in your React applications.
Page navigation in React Router involves transitioning between different routes and rendering the corresponding components. With React Router, you can easily navigate between pages using links or programmatically using the navigate
function from the useNavigate
hook. Let's dive into the world of page navigation and unlock its superpowers!
Page navigation in React Router refers to the process of transitioning between different routes and rendering the appropriate components based on the current route. It involves handling user interactions, such as clicking on links, and updating the URL to reflect the current route.
Page navigation in React Router offers several benefits:
Here are the steps to navigate between pages in React Router:
Router
component and define your routes using the Route
component.Link
component to create clickable links that navigate to different routes.navigate
function from the useNavigate
hook.Let's define our routes:
// App.js
import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import Home from './Home';
import About from './About';
function App() {
return (
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</Router>
);
}
export default App;
In this code, we define two routes: one for the homepage (/
) and another for the about page (/about
). We use the element
prop to specify the component that should be rendered for each route.
Now, let's use the Link
component to create clickable links that navigate between pages:
// Home.js
import React from 'react';
import { Link } from 'react-router-dom';
function Home() {
return (
<div>
Welcome to the Home page!
<Link to="/about">Go to About page</Link>
</div>
);
}
export default Home;
In this code, we import the Link
component from react-router-dom
. We use the Link
component to create a clickable link that navigates to the about page (/about
) when clicked.
React Router also allows you to navigate programmatically using the navigate
function from the useNavigate
hook:
// About.js
import React, { useNavigate } from 'react';
function About() {
const navigate = useNavigate();
return (
<div>
This is the About page!
<button onClick={() => navigate('/')}>Go to Home page</button>
<button onClick={() => navigate(-1)}>Go back one page</button>
</div>
);
}
export default About;
In this code, we import the useNavigate
hook to access the navigate
function. We use the navigate
function to programmatically navigate to different routes. For example, navigate('/')
navigates to the homepage, and navigate(-1)
goes back one page in the browser history.
Let's see the complete example:
// App.js
import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import Home from './Home';
import About from './About';
function App() {
return (
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</Router>
);
}
export default App;
// Home.js
import React from 'react';
import { Link } from 'react-router-dom';
function Home() {
return (
<div>
Welcome to the Home page!
<Link to="/about">Go to About page</Link>
</div>
);
}
export default Home;
// About.js
import React, { useNavigate } from 'react';
function About() {
const navigate = useNavigate();
return (
<div>
This is the About page!
<button onClick={() => navigate('/')}>Go to Home page</button>
<button onClick={() => navigate(-1)}>Go back one page</button>
</div>
);
}
export default About;
Router
, Routes
, and Route
components.Link
component to create clickable links that navigate between pages.navigate
function from the useNavigate
hook to programmatically navigate to different routes. The useNavigate
hook provides a way to navigate programmatically and also allows us to go back in the browser history using negative values.React Router provides a seamless navigation experience in your React applications. By using links or programmatically navigating, users can transition between pages without full page reloads, resulting in a more intuitive and engaging user experience. The useNavigate
hook enhances this experience by providing a way to programmatically navigate and manipulate the browser history.