Hi, Developers! You will learn how to build React navigation using UseNavigate in this article. Let's see why useNavigate is the best way to handle navigation in React apps.
In a React app, React Navigation is important for smooth navigation. While useHistory used to be the go-to for handling navigation history, useNavigate has become the preferred option now. In React Router v6, useNavigate takes over from useHistory, providing improved navigation control.
The useHistory hook in React Router serves as a navigation assistant, guiding users through the history of your web application. Since its introduction in version 5, developers have relied on it to seamlessly move users forward, backward, or replace pages using methods like push, goBack, and replace.
However, as React Router continues to evolve, newer versions like v6 introduce even more efficient alternatives, such as the useNavigate hook.
Understanding useNavigate Hooks
In React Router version 6, they added a new hook called 'useNavigate'. It's like a tool that helps you move around your website. You can think of it as a simpler way to change where you are on the site. Before, there was a tool called 'useHistory', but now they're saying 'useNavigate' is better, so they're not using 'useHistory' anymore.
Let's jump into a real project where we'll use the useNavigate hook. In this practical exercise, you will find out how you can use it in your own project.
Implementing useNavigate for Enhanced Routing Efficiency
When you want to use the useNavigate hook in your project, you'll first need to import it from react-router-dom into the specific file where you plan to use it.
import { useNavigate } from 'react-router-dom';
Once imported, you can simply call the useNavigate hook inside your functional component to access its functionalities and efficiently manage navigation within your application.
Below is a simple example demonstrating the basic implementation of the useNavigate hook in a React application:
Home.jsx
// Home.jsx
import React from 'react';
import { useNavigate } from 'react-router-dom';
const HomePage = () => {
const navigate = useNavigate();
// Navigate to the '/about' route
const handleButtonClick = () => navigate('/about');
return (
<div>
<h1>Home Page</h1>
<button onClick={handleButtonClick}>Go to About Page</button>
</div>
);
};
export default HomePage;
In this example, we have a simple functional component called HomePage. We import the useNavigate hook from react-router-dom at the top of the file. Inside the component, we use the useNavigate hook to get the navigate function.
When the button is clicked, the handleButtonClick function is invoked, which calls the navigate function with the path to the '/about' route. This effectively navigates the user to the About page.
We'll follow a similar approach to creating the About page.
About.jsx
// About.jsx
import React from 'react'
import { useNavigate } from 'react-router-dom';
const AboutPage = () => {
const navigate = useNavigate();
// Navigate to the '/about' route
const handleButtonClick = () => navigate('/');
return (
<div>
<h1>About Page</h1>
<button onClick={handleButtonClick}>Go to Home Page</button>
</div>
);
};
export default AboutPage;
In this example, we use the useNavigate hook from react-router-dom to navigate within a functional component called AboutPage. When the button is clicked, the handleButtonClick function is invoked, which calls the navigate function with the path to the main route (Home), effectively taking the user back to the Home page.
Now, let's configure this route in our App.js file.
// App.js
import React from 'react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import About from './components/About';
import Home from './components/Home';
const App = () => (
<>
<Router>
<Routes>
<Route exact path="/" element={<Home />} />
<Route exact path="/about" element={<About />} />
</Routes>
</Router>
</>
);
export default App;
This code sets up navigation in a React app using 'react-router-dom'. It creates paths for the Home and About sections so users can move between them easily.
To enhance how users move around our React app, we can use the useNavigate hook.
Benefits of Replacing from useHistory to useNavigate
Simplified Navigation: useNavigate simplifies navigation by providing a single function for route changes, making it easier to manage compared to useHistory.
Intuitive Interface: Unlike useHistory, which requires multiple methods like push, goBack, and replace, useNavigate offers a more intuitive interface with a single function call.
Enhanced Readability: With useNavigate, code becomes more concise and readable, reducing complexity and improving maintainability compared to multiple useHistory methods.
Improved Type Safety: useNavigate provides better type safety, reducing the risk of runtime errors compared to useHistory, making it a safer choice for navigation in React Router v6.
Recommended Approach: React Router v6 recommends useNavigate as the primary method for navigation, ensuring compatibility and future-proofing for your application.
Using useNavigate instead of useHistory makes navigation easier, safer, and more intuitive in React apps. As a result, developers work more efficiently, and users have a better experience with the app.
Conclusion
In simple terms, replacing useHistory with useNavigate makes navigating in React apps easier. By grasping and using these hooks, especially useNavigate, we can make navigating our React apps much easier for users.
Switching from useHistory to useNavigate is simple. Developers can update their code gradually, replacing useHistory with useNavigate. It aligns with React Router v6's latest best practices.
If you have any questions regarding this article or any other web development, then you can ask them in the question box given below, and you will get the answer soon.