Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/marvinhagemeister/preact-simple-router

Simple router for preact
https://github.com/marvinhagemeister/preact-simple-router

preact rlite router simple tiny

Last synced: 27 days ago
JSON representation

Simple router for preact

Awesome Lists containing this project

README

        

# Preact Simple Router

A simple router for [preact](https://github.com/developit/preact/) based on
[rlite](https://github.com/chrisdavies/rlite) that can load both sync and async
routes.

**Note:** Although it works I'm currently not happy with the api and will likely
change the internals quite a bit to allow easier testing. Treat this package
as alpha!

## Installation

```bash
# npm
npm install --save preact preact-simple-router

# yarn
yarn add preact preact-simple-router
```

## Usage

```jsx
import { h } from "preact";
import { Router}  from "preact-simple-router";

const routes = {
"/": () => import("./MyHomeComponent").then(m => m.default()),
"/about": () => import("./About").then(m => m.default()),
// Passing :name to component
"/blog/:name": (args) => import("./MyBlogPost").then(m => m.default(args.name)),
// Will be automatically used as error route
"/404": () => import("../404").then(m => m.default()),
};

export default function App() {
return "Loading failed"} // if network is down, etc
error={err => "failed " + err.message} // if an unknown error occured
/>;
}
```

Linking to other routes (the `url` must be **absolute**!):

```jsx
import { h } from "preact";
import { Link} from "preact-simple-router";

export default function App() {
return



My First Blog Post

;
}
```

## License

`MIT`, see [License file](LICENSE.md).