7

Since in react-router-dom useHistory is no longer supported, I am looking for an equivalent to use history.location.pathname

Fraction
  • 11,668
  • 5
  • 28
  • 48
pratibimbam
  • 91
  • 1
  • 5
  • use this link https://stackoverflow.com/questions/63471931/using-history-with-react-router-dom-v6 – A.R.SEIF Dec 23 '21 at 10:57
  • TL;DR Use [useNavigate](https://reactrouter.com/docs/en/v6/api#usenavigate). For other changes see the [v5 migration guide](https://reactrouter.com/docs/en/v6/upgrading/v5). – Drew Reese Dec 23 '21 at 11:04

1 Answers1

13

You have to use useLocation ¹, ² which returns a location object that contains the pathname and other information about the URL:

const location = useLocation();

console.log(location)
// {
//   pathname: "/invoices",
//   search: "?filter=sa",
//   hash: "",
//   state: null,
//   key: "ae4cz2j"
// }

Fraction
  • 11,668
  • 5
  • 28
  • 48