Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for useParam hook #74

Merged
merged 2 commits into from
Jun 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions docs/route-params-query.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ sidebar_label: URL Params & Query
This hook returns all the parameters for the current route as an object. Parameter types are `string` or `string[]`

```tsx
// Page: app/products/pages/products/[id].tsx
// URL: /app/products/pages/products/2
// File: app/products/pages/products/[id].tsx
// URL: /products/2

import {useParams} from "blitz"

Expand All @@ -19,20 +19,46 @@ const params = useParams()
```

```tsx
// Page: app/pages/blog/[...slug].tsx
// URL: /app/pages/blog/2020/1
// File: app/pages/blog/[...slug].tsx
// URL: /blog/2020/1

const params = useParams()

// params = {slug: ["2020", "1"]}
```

## `useParam`

This hook returns a single parameter cast to chosen type. Available types are: `number`, `string` and `array` which always returns an array of strings.

```tsx
// File: app/locations/pages/locations/[id]/[[...zipcode]].tsx
// URL: /locations/2/02137

import {useParam} from "blitz"

const id = useParam("id", "number")
const zipcodes = useParam("zipcode", "array")

// id = 2
// zipcodes = ["02137"]
```

```tsx
// File: app/pages/blog/[slug].tsx
// URL: /blog/hello-world

const slug = useParam("slug", "string")

// slug = "hello-world"
```

## `useRouterQuery`

This hook returns all the query parameters from the URL as an object. Parameter type is always `string`

```tsx
// URL: /products?sort=asc&limit=10
// URL: /products?sort=asc&limit=10

import {useRouterQuery} from "blitz"

Expand Down