Skip to content

Instantly share code, notes, and snippets.

@helloncanella
Last active July 9, 2022 15:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helloncanella/707b4241a7e4868d82e67b4b1ebd4a4b to your computer and use it in GitHub Desktop.
Save helloncanella/707b4241a7e4868d82e67b4b1ebd4a4b to your computer and use it in GitHub Desktop.
import useRouter from "use-react-router"
import { matchPath } from "react-router-dom"
export default function useParams(path) {
const { location } = useRouter()
const { pathname } = location
const pattern = `(.*)?${path}`
const match = matchPath(pathname, { path: pattern }) || {}
return match.params
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment