17 breaking releases

0.18.0 Sep 29, 2023
0.17.0 Nov 25, 2022
0.16.0 Dec 11, 2021
0.15.0 May 15, 2021
0.3.0 Jul 24, 2019

#1996 in Web programming

Download history 4716/week @ 2024-07-21 4245/week @ 2024-07-28 4343/week @ 2024-08-04 4852/week @ 2024-08-11 4347/week @ 2024-08-18 4853/week @ 2024-08-25 4722/week @ 2024-09-01 3894/week @ 2024-09-08 3372/week @ 2024-09-15 3860/week @ 2024-09-22 4592/week @ 2024-09-29 3133/week @ 2024-10-06 4618/week @ 2024-10-13 4130/week @ 2024-10-20 3646/week @ 2024-10-27 3524/week @ 2024-11-03

16,186 downloads per month
Used in 27 crates (23 directly)

MIT/Apache

610KB
14K SLoC

yew-router

A routing library for the Yew frontend framework.

Read on how to use it on yew.rs


lib.rs:

Provides routing faculties using the browser history API to build Single Page Applications (SPAs) using Yew web framework.

Usage

use yew::functional::*;
use yew::prelude::*;
use yew_router::prelude::*;

#[derive(Debug, Clone, Copy, PartialEq, Routable)]
enum Route {
    #[at("/")]
    Home,
    #[at("/secure")]
    Secure,
    #[not_found]
    #[at("/404")]
    NotFound,
}

#[function_component(Secure)]
fn secure() -> Html {
    let navigator = use_navigator().unwrap();

    let onclick_callback = Callback::from(move |_| navigator.push(&Route::Home));
    html! {
        <div>
            <h1>{ "Secure" }</h1>
            <button onclick={onclick_callback}>{ "Go Home" }</button>
        </div>
    }
}

#[function_component(Main)]
fn app() -> Html {
    html! {
        <BrowserRouter>
            <Switch<Route> render={switch} />
        </BrowserRouter>
    }
}

fn switch(routes: Route) -> Html {
    match routes {
        Route::Home => html! { <h1>{ "Home" }</h1> },
        Route::Secure => html! {
            <Secure />
        },
        Route::NotFound => html! { <h1>{ "404" }</h1> },
    }
}

Internals

The router registers itself as a context provider and makes location information and navigator available via hooks or RouterScopeExt.

State

The Location API has a way to access / store state associated with session history. Please consult location.state() for detailed usage. A module that provides universal session history and location information. Prelude module to be imported when working with yew-router.

This module re-exports the frequently used types from the crate.

Dependencies

~12–21MB
~290K SLoC