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

https://github.com/liascode/preact-hashish-router

Simple preact router with hash and browser routing
https://github.com/liascode/preact-hashish-router

Last synced: about 1 month ago
JSON representation

Simple preact router with hash and browser routing

Awesome Lists containing this project

README

        

# Preact Hashish Router

## Features

- `hash` and `browser` routing types.
- Support for lazy-loaded routes (`lazy` loading).
- Error handling integration with `ErrorRoute`.
- Fully typed.
- Ultra lightweight.
- No external dependencies.

## Installation

```bash
npm install preact-hashish-router@latest
```

## Usage

### Context Setup

First, ensure your application is wrapped within the router context. This will allow you to access routes and related functions.

```tsx
import { ErrorRoute, Route, Router, RouterErrorBoundary } from "preact-hashish-router";
import _404 from "./routes/404";
import AboutPage from "./routes/About";
import HomePage from "./routes/Home";
import ProductPage from "./routes/Product";

export default function App() {
return (
{/* <-- or browser */}








<_404 />



);
}
```

### Using the `useRouter` Hook

The `useRouter` hook gives you access to the router context to programmatically navigate or retrieve information about the current route.

```tsx
import { useRouter } from "preact-hashish-router";

function HomePage() {
const router = useRouter();

function goToAbout() {
router.go("/about");
}

return (


Home Page


Go to About

);
}
```

### `` Component for Navigation

```tsx
import { A } from "preact-hashish-router";

export default function Header() {
return (


Home
About


);
}
```

### `` Component

```tsx
import { Redirect } from "preact-hashish-router";

export default function ProductPage() {
return (
<>


Home
About



>
);
}
```

## Development

If you have any improvements or find any issues, feel free to contribute or open an issue in the associated repository.