> For the complete documentation index, see [llms.txt](https://atomico.gitbook.io/doc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://atomico.gitbook.io/doc/packages/atomico-router.md).

# @atomico/router

powerful router for Webcomponents, React and Preact

@atomico/router aims to facilitate the creation of SPA-type applications through the following strategies:

1. &#x20;**slot**: you can reference a slot within the Router Switch to show on a specific path.
2. **elements**: you will be able to mount CustomElements through Instantable elements.
3. **asynchronous generators**: through asynchronous generators you will be able to show multiple views according to the load state, for example transitions.&#x20;

{% embed url="<https://github.com/atomicojs/router>" %}

## Syntax

{% tabs %}
{% tab title="Atomico JSX" %}

```jsx
import { render } from "atomico";
import { RouterSwitch, RouterCase } from "@atomico/router";

render(
    <RouterSwitch>
        <RouterCase path="/" for="home"></RouterCase>
        <RouterCase path="/user/{id}" load={async function *()({id}){
            yield <h1>loading...</h1>;
            const data = await getUser(id);
            return <User {...data}/>
        }}></RouterCase>
        <h1 slot="config">home</h1>
    </RouterSwitch>,
    document.body
)
```

{% endtab %}

{% tab title="HTML" %}

```html
<router-switch>
    <router-case path="/" for="home"></router-case>
    <router-case path="/config" for="config"></router-case>
    <section slot="home">...</section>
    <section slot="config">...</section>
</router-switch>
```

{% endtab %}
{% endtabs %}

## Elements

### RouterSwitch

#### Properties

<table><thead><tr><th>Props</th><th width="358">Description</th><th width="184">Type</th><th>Event</th></tr></thead><tbody><tr><td>case</td><td>slot to associate at the time of the match with path</td><td>String - read only</td><td>match</td></tr></tbody></table>

#### Events

| Event | JSX     |                                                             |
| ----- | ------- | ----------------------------------------------------------- |
| Match | onMatch | It is dispatched every time the router matches a new route. |

### RouterCase

Component that declares the behavior of the route, with it you can:

1. Define a slot to show instantly when matching the browser path.
2. Define a load callback to execute when matching the browser route.

#### Properties

| Props   | Description                                                          | Type     |
| ------- | -------------------------------------------------------------------- | -------- |
| load    | asynchronous content loader                                          | Function |
| for     | slot to associate at the time of the match with path                 | String   |
| path    | expression for path                                                  | String   |
| memo    | memorize the state resolved by load according to the concurrent path | Boolean  |
| destroy | Remove associated view on route change                               | Boolean  |

## Examples

### Pokeapi

{% embed url="<https://stackblitz.com/edit/atomico-router?embed=1&file=package.json&view=preview>" %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://atomico.gitbook.io/doc/packages/atomico-router.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
