# react-hooks-router

> A react hooks approach to routing

Latest version **0.3.2** (published 2019-09-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-hooks-router
pnpm add react-hooks-router
yarn add react-hooks-router
bun add react-hooks-router
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2019-09-24 |
| First published | 2018-11-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 15.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Garth Williams |
| Maintainers | garthw |
| Keywords | react, router, hooks |

## Links

- npm: https://www.npmjs.com/package/react-hooks-router
- Repository: https://github.com/garth/react-hooks-router
- Homepage: https://github.com/garth/react-hooks-router#readme
- Issues: https://github.com/garth/react-hooks-router/issues
- npm.io page: https://npm.io/package/react-hooks-router

## Dependencies (2)

- [page](https://npm.io/package/page.md) ^1.11.4
- [tslib](https://npm.io/package/tslib.md) ^1.10.0

## Recent versions

- 0.3.2 (latest) — 2019-09-24
- 0.3.1 — 2019-07-31
- 0.2.2 — 2019-07-31
- 0.3.0 — 2019-05-17
- 0.2.1 — 2018-12-06
- 0.2.0 — 2018-12-06
- 0.1.1 — 2018-12-03
- 0.1.0 — 2018-11-29

## README

# react-hooks-router

A react hooks approach to routing.

This library is a simple wrapper around page.js.

## Install

```
yarn add react-hooks-router
```

or

```
npm install react-hooks-router
```

## Define Routes

```js
import { route } from 'react-hooks-router'

export const routes = {
  user: (params: { id: string }) => route('/user/:id', params),
  faq: '/faq',
  login: '/login',
  notFound: '*'
}
```

## Use The Hook

```tsx
import React, { useState } from 'react'
import { routes } from './routes'
import { useRouter } from 'react-hooks-router'

const App: React.FunctionComponent = () => {
  const route = useRouter({ routes })
  return (
    <div>
      <h1>{route.name}</h1>
    </div>
  )
}

export default App
```

## Optional OnRouted Callbacks

Optional `onRouted` and `onRoutedUnhandled` parameters will be invoked for every matching route change

```ts
const onRoutedUnhandled = ({ name, props, path }) => {
  console.log(name, 'routed with props', props)
}

const App: React.FunctionComponent = () => {
  const route = useRouter({ routes, onRoutedUnhandled })
  // ...
}
```

or a collection of callbacks, one for each route

```ts
const onRouted = {
  user: ({ name, props, path }) => {},
  faq: ({ name, props, path }) => {},
  login: ({ name, props, path }) => {},
  notFound: ({ name, props, path }) => {}
}

const App: React.FunctionComponent = () => {
  const route = useRouter({ routes, onRouted })
  // ...
}
```

## Initial Route

When your component is first rendered and before the routing effect has fired, there will be a moment when the current route has not yet been determined. By default useRouter will return `{ name: 'loading', params: {} }` until the actual route is determined. You can change this by passing in an `initialRoute`.

```ts
const App: React.FunctionComponent = () => {
  const route = useRouter({ initialRoute: { name: 'home', params: {}, path: '' }, routes })
  // ...
}
```

---
_Source: https://npm.io/package/react-hooks-router · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
