react-router-dom v6 useHistory location.pathname

Viewed 1643

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

1 Answers

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"
// }

Related