Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mskelton/use-strict-params

Strictly typed params for React router.
https://github.com/mskelton/use-strict-params

params react react-router strict typescript

Last synced: 30 days ago
JSON representation

Strictly typed params for React router.

Awesome Lists containing this project

README

        

# use-strict-params

[![Build status](https://github.com/mskelton/use-strict-params/workflows/Build/badge.svg)](https://github.com/mskelton/use-strict-params/actions)
[![npm version](https://img.shields.io/npm/v/use-strict-params)](https://www.npmjs.com/package/use-strict-params)

Strictly typed params for React router.

## Installation

### npm

```sh
npm install use-strict-params
```

### Yarn

```sh
yarn add use-strict-params
```

### pnpm

```sh
pnpm add use-strict-params
```

### bun

```sh
bun add use-strict-params
```

## Usage

### `useStrictParams`

A typed variant of `useParams`. The following types are supported:

- `String`
- `Number`
- `Boolean`
- `Date`

```typescript
const { userId } = useStrictParams({ userId: Number })
```

### `useStrictSearchParams`

A typed variant of `useSearchParams`. The following types are supported:

- `String`
- `Number`
- `Boolean`
- `Date`

```typescript
const { query } = useStrictSearchParams({ query: String })
```

### `Optional`

Marks a type as optional which will change the return type to allow `null`.

```typescript
const { query } = useStrictSearchParams({ query: Optional(String) })
```