AKA: useLastPath
๐ฅ A React Hook containing the last path from a Next.js route + some goodies ๐
- โก๏ธ Fast
- ๐งฉ Small (1.1kB gzipped).
- ๐ฅถ Dependency free.
- ๐งช Based on regex.
- ๐งจ Specially useful when working with dynamic routes.
npm i next-last-pathimport { useLastPath } from 'next-last-path'
const Component = (props: any) => {
const { lastPath, isLastPath, isDynamic, query, isMatch } = useLastPath()
return <div>
the last path is: {lastPath} {isDynamic && `and it's dynamic`}
</div>
}
export default Component| Option | Type | Description |
|---|---|---|
| matcher | RegExp | string | Record<string, RegExp | string> | A RegExp, a string, or an Object with RegExp or strings as values. Perform a match on the lastPath |
| allowBrackets | boolean | If route is dynamic, allow brackets on the final result of lastPath. |
| allowDots | boolean | If route is dynamic, allow dots on the final result of lastPath. |
| defaultHome | string | By default the homepage is returned as /. If you are in / instead defaultHome will be returned. |
