useHistory Hook in React for App Navigation
Tags: react, beginners, webdev, hooks
Published: 2023-01-09 18:52:07


In my most recent project, my partner and I built a (faux) art auction site, Rotheby's. One of the features we wanted to implement was the ability of a user to click on the "Place Bid" button a specific piece of artwork, and for the user to be re-routed/directed to a new page displaying that individual piece of artwork and a form to place a bid. To implement this functionality, we called upon React's useHistory hook, and more specifically, the .push() method.
 
Auction Homepage with all artworks:
![Artworks Homepage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q452dah1nryv9a9hxrmt.png)

Re-directed page with individual artwork:
![Individual Artwork](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ipg4q9yzoqxraf4b40ny.png)

## Background on React's History Object & useHistory Hook
The history package is a major dependency of ReactRouter that provides various implementations for managing session history. The useHistory hook accesses the ReactRouter history object and allows navigation to other routes using specific methods, such as .push() and .replace().

With the useHistory hook, a user can efficiently and seamlessly navigate to a specific URL, and move forward and back on different pages within an app. For my partner and my art auction application, we used the .push() method, which adds a new entry to the history stack. This provided us a way to programmatically control the browser's history and change the URL displayed in the address bar without reloading the page. 

## Implementation of useHistory and .push()
In the Artwork component (in our React app): 
- we imported the useHistory hook at the top of the component, which gave us access to the useHistory functionality 
- we declared the "history" variable to the useHistory hook
- we defined the handleRedirect function which includes the history variable, the .push() method, and the route we want to navigate to. For this project we wanted to re-direct the user to the specific artwork page (each artwork in our database had an id value associated with it)
- we assigned the handleRedirect function to the onClick event handler on the "Place Bid" button

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

 const history = useHistory();

 function handleRedirect() {
   // redirect to /artworks/:id
   history.push(`/artworks/${id}`);
 }

return(
     <button className="bidButton" onClick={handleRedirect}>
            Place Bid
     </button>
); 
```

Once the user clicks on the "Place Bid" button, they are re-directed to the URL with that specific artwork's id and the new page with the artwork's details and the form to bid on that piece of artwork. 

_** Note:_ the route (/artworks/:id) is associated with a route declared in a higher-level component (App.js) which routes to the ArtworkDetails component containing the code for the artwork's details and bidding form. 

All Artwork URL: 
![All Artwork URL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2r2jogji84b1b4r953rm.png)


Individual Artwork URL (the artwork selected has id=1): 
![Individual Artwork URL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhwk3g6qwjmrzmtichzy.png)

## Wrap Up
Learning about the useHistory hook and .push() methods were vital for my partner and I to successfully build the auction site we had imagined. I hope this blog post was helpful in providing a quick breakdown of the topic and associated example! 

Happy Coding!








My partner for this project was:

{% embed https://dev.to/racheljoyh %}