The useNavigate hook in a React Router application allows for programmatic navigation between different routes within the app. It returns a function that can be called with a specific path to navigate to, enabling dynamic routing based on events or user actions, such as form submissions or button clicks.
How to use the useNavigate hook in React router
Key takeaways:
The
useNavigatehook, part of thereact-router-dompackage, facilitates programmatic navigation within a React application by returning anavigatefunction.The
useNavigatehook can only be used inside functional components that are nested within a Router instance.In a typical implementation,
useNavigateenables seamless transitions between pages, such as redirecting users based on specific input or user actions.Effective use of the
useNavigatehook enhances user experience by simplifying routing and creating interactive applications.
What is a hook in React?
Hooks are special functions in React that allow you to “hook into” React’s state and life cycle features from functional components. They enable functionalities like managing state, side effects, and context, without needing to use class components. Common hooks include useState for managing state and useEffect for handling side effects.
You can enhance your knowledge regarding React hooks by exploring the “What are React hooks?” Answer.
What is the useNavigate hook?
The useNavigate hook is part of the react-router-dom package that allows for programmatic routing inside a React application. This hook returns a function (navigate) that can be invoked with a Router instance.
If you're new to React, consider reading our detailed blog “A Step-by-Step Guide To Learn React” to get familiar with core concepts like functional components and hooks, which will help you better understand the concepts given in this Answer.
Coding example
The code snippet below demonstrates the use of the useNavigate hook inside a React application.
The example application defines two pages: LoginPage on the / route and HomePage on the /home route inside the index.js file. When the user enters the SECRET_KEY (Educative Rocks!) inside the text box, they are navigated to the HomePage using the useNavigate hook.
The LoginPage
Line 1: Imports the
useNavigatehook from thereact-router-dompackage.Line 3: Defines the
SECRET_KEYthrough which the user can switch to theHomePage.Line 6: Invokes the
useNavigatehook to get thenavigatefunction.Lines 8–12: Defines the function
handleChangethat checks whether the user has entered the correct secret key. If so, thenavigatefunction is invoked with the/homepath to route the user to theHomePage.Lines 14–20: Defines the layout of the
LoginPagewith a heading, some text, and a textbox. ThehandleChangefunction is attached to theinput, causing it to be invoked whenever the user updates the text inside the text box.
The HomePage
Line 1: Imports the
useNavigatehook from thereact-router-dompackage.Line 4: Invokes the
useNavigatehook to get thenavigatefunction.Lines 6–12: Defines the layout of the
HomePagewith a heading, some text, and a button to go back to theLoginPage. This is achieved by attaching() => navigate('/')as theonClickmethod for the button.
Knowledge test
Let’s attempt a short quiz to assess your understanding.
Which statement about the useNavigate hook in React is correct?
The useNavigate hook can be used inside both class components and functional components.
The useNavigate hook returns a function that can be used to programmatically navigate to different routes.
The useNavigate hook is part of the core React library.
The useNavigate hook can be used outside of a Router instance.
Conclusion
The useNavigate hook in React Router simplifies navigation within your application, enabling seamless transitions between pages. By implementing this hook effectively, you can enhance user experience and create a more interactive React app.
Frequently asked questions
Haven’t found what you were looking for? Contact Us
What is the function of the `useNavigate` hook in a react-router application?
Why use `useNavigate` instead of `Link`?
What is the difference between `useNavigate` and `redirect` in React?
Can I use `useNavigate in `useEffect`?
Free Resources