DEV Community

Saleh Mubashar
Saleh Mubashar

Posted on • Edited on

11 2

How to use React History in React JS

Hi guys.

Ok so today we I will give you all the simple steps to use
Browser History in React JS.
Now you might be wondering, what is it for?
**React Router History **can be used to go to a previous page or to go to the appropriate response page, for example after a user logs in, he should be redirected to the dashboard page.

Step 1

Install react router dom.



npm install --save react-router-dom


Enter fullscreen mode Exit fullscreen mode

Step 2

Import the history package from react router dom.



import { useHistory } from "react-router-dom"


Enter fullscreen mode Exit fullscreen mode

Step 3

Assign the history function to a variable (not necessary but
recommended)



const history = useHistory()


Enter fullscreen mode Exit fullscreen mode

Step 4

Use the push() function to redirect the user after a successful login, for example.



history.push("/dashboard")


Enter fullscreen mode Exit fullscreen mode

Here we are sending the user to the dashboard.


And there you go!.
Thank you all for reading this post.
You can find the same article with a little more explanation and detail at HubPages. React History Hubpages
If it was helpful, please consider following :)
Until next time
Bye!

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay