get-params
Uses next.js functionality
Retrieves dynamic parameters for the current page. Works the same as useParams, but in a React server component.
For example, for a page at the directory /[locale]/blog/[slug]
and pathname /en-de/blog/example
- the getter will return { locale: 'en-de', slug: 'example' }
How to
import { getParams } from '@nimpl/getters/get-params'
export default function Component() {
const params = getParams()
return (
// ...
)
}
Options
You can pass params object as arguments to getParams.
ignoreDifferenceError
- this option disables difference errors, and in such situations, the function will simply return null
.
pagePaths
- custom page paths for parsing the current pathname (e.g., ["/specific/[locale]/[...subpaths]/page", "/[locale]/base/[...subpaths]/page"]
).
const params = getParams({
ignoreDifferenceError: true,
pagePaths: ["/specific/[locale]/[...subpaths]/page", "/[locale]/base/[...subpaths]/page"],
});
// "/specific/de/unknown-page" -> { locale: "de", subpaths: ["unknown-page"] }
// "/it/base/unknown-page" -> { locale: "it", subpaths: ["unknown-page"] }
// "/it/invalid/unknown-page" -> null