Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/developit/preact-router

:earth_americas: URL router for Preact.
https://github.com/developit/preact-router

preact preact-components preact-router router

Last synced: 2 days ago
JSON representation

:earth_americas: URL router for Preact.

Awesome Lists containing this project

README

        

# preact-router

[![NPM](https://img.shields.io/npm/v/preact-router.svg)](https://www.npmjs.com/package/preact-router)
[![Build status](https://github.com/preactjs/preact-router/actions/workflows/node.js.yml/badge.svg)](https://github.com/preactjs/preact-router/actions/workflows/node.js.yml)

> [!WARNING]
> `preact-router` unfortunately no longer sees active development! It's completely stable and so you can rely upon it for all existing apps, but for newer ones, we'd recommend using [`preact-iso`](https://github.com/preactjs/preact-iso) for your routing needs instead. It offers a very similar API while integrating a bit better Suspense and lazy loading, with potentially more useful hooks. Thanks to all the contributors and users over the years!

Connect your [Preact](https://github.com/preactjs/preact) components up to that address bar.

`preact-router` provides a `` component that conditionally renders its children when the URL matches their `path`. It also automatically wires up `` elements to the router.

> 💁 **Note:** This is not a preact-compatible version of React Router. `preact-router` is a simple URL wiring and does no orchestration for you.
>
> If you're looking for more complex solutions like nested routes and view composition, [react-router](https://github.com/ReactTraining/react-router) works great with preact as long as you alias in [preact/compat](https://preactjs.com/guide/v10/getting-started#aliasing-react-to-preact).

#### [See a Real-world Example :arrow_right:](https://jsfiddle.net/developit/qc73v9va/)

---

### Usage Example

```js
import Router from 'preact-router';
import { h, render } from 'preact';
/** @jsx h */

const Main = () => (



// Advanced is an optional query


);

render(, document.body);
```

If there is an error rendering the destination route, a 404 will be displayed.

#### Caveats

Because the `path` and `default` props are used by the router, it's best to avoid using those props for your component(s) as they will conflict.

### Handling URLS

:information_desk_person: Pages are just regular components that get mounted when you navigate to a certain URL.
Any URL parameters get passed to the component as `props`.

Defining what component(s) to load for a given URL is easy and declarative.
Querystring and `:parameter` values are passed to the matched component as props.
Parameters can be made optional by adding a `?`, or turned into a wildcard match by adding `*` (zero or more characters) or `+` (one or more characters):

```js







```

### Lazy Loading

Lazy loading (code splitting) with `preact-router` can be implemented easily using the [AsyncRoute](https://www.npmjs.com/package/preact-async-route) module:

```js
import AsyncRoute from 'preact-async-route';


import('./friends').then(module => module.default)}
/>
import('./friend').then(module => module.default)}
loading={() =>

loading...
}
/>
;
```

### Active Matching & Links

`preact-router` includes an add-on module called `match` that lets you wire your components up to Router changes.

Here's a demo of ``, which invokes the function you pass it (as its only child) in response to any routing:

```js
import Router from 'preact-router';
import Match from 'preact-router/match';

render(


{({ matches, path, url }) =>
{url}
}

demo fallback route



);

// another example: render only if at a given URL:

render(


{({ matches }) => matches &&

You are Home!

}


);
```

`` is just a normal link, but it automatically adds and removes an "active" classname to itself based on whether it matches the current URL.

```js
import { Router } from 'preact-router';
import { Link } from 'preact-router/match';

render(




Home


Foo


Bar



this is a demo route that always matches



);
```

### Default Link Behavior

Sometimes it's necessary to bypass preact-router's link handling and let the browser perform routing on its own.

This can be accomplished by adding a `data-native` boolean attribute to any link:

```html
Foo
```

### Detecting Route Changes

The `Router` notifies you when a change event occurs for a route with the `onChange` callback:

```js
import { render, Component } from 'preact';
import { Router, route } from 'preact-router';

class App extends Component {
// some method that returns a promise
isAuthenticated() {}

handleRoute = async e => {
switch (e.url) {
case '/profile':
const isAuthed = await this.isAuthenticated();
if (!isAuthed) route('/', true);
break;
}
};

render() {
return (




);
}
}
```

### Redirects

Can easily be implemented with a custom `Redirect` component;

```js
import { Component } from 'preact';
import { route } from 'preact-router';

export default class Redirect extends Component {
componentWillMount() {
route(this.props.to, true);
}

render() {
return null;
}
}
```

Now to create a redirect within your application, you can add this `Redirect` component to your router;

```js


```

### Custom History

It's possible to use alternative history bindings, like `/#!/hash-history`:

```js
import { h } from 'preact';
import Router from 'preact-router';
import { createHashHistory } from 'history';

const Main = () => (





);

render(, document.body);
```

### Programmatically Triggering Route

It's possible to programmatically trigger a route to a page (like `window.location = '/page-2'`)

```js
import { route } from 'preact-router';

route('/page-2'); // appends a history entry

route('/page-3', true); // replaces the current history entry
```

### Nested Routers

The `` is a self-contained component that renders based on the page URL. When nested a Router inside of another Router, the inner Router does not share or observe the outer's URL or matches. Instead, inner routes must include the full path to be matched against the page's URL:

```js
import { h, render } from 'preact';
import Router from 'preact-router';

function Profile(props) {
// `props.rest` is the rest of the URL after "/profile/"
return (


Profile







);
}
const MyProfile = () =>

My Profile

;
const UserProfile = props =>

{props.user}

;

function App() {
return (







Home
My Profile
Alice's Profile


);
}

render(, document.body);
```

### `Route` Component

Alternatively to adding the router props (`path`, `default`) directly to your component, you may want to use the `Route` component we provide instead. This tends to appease TypeScript, while still passing down the routing props into your component for use.

```js
import { Router, Route } from 'preact-router';

function App() {
let users = getUsers();

return (


{/* Route will accept any props of `component` type */}


);
}
```

### License

[MIT](./LICENSE)