This is unreleased documentation for Yew Next version.
For up-to-date documentation, see the latest version on docs.rs.

yew_router/
lib.rs

1//! Provides routing faculties using the browser history API to build
2//! Single Page Applications (SPAs) using [Yew web framework](https://yew.rs).
3//!
4//! # Usage
5//!
6//! ```rust
7//! use yew::functional::*;
8//! use yew::prelude::*;
9//! use yew_router::prelude::*;
10//!
11//! #[derive(Debug, Clone, Copy, PartialEq, Routable)]
12//! enum Route {
13//!     #[at("/")]
14//!     Home,
15//!     #[at("/secure")]
16//!     Secure,
17//!     #[not_found]
18//!     #[at("/404")]
19//!     NotFound,
20//! }
21//!
22//! #[function_component(Secure)]
23//! fn secure() -> Html {
24//!     let navigator = use_navigator().unwrap();
25//!
26//!     let onclick_callback = Callback::from(move |_| navigator.push(&Route::Home));
27//!     html! {
28//!         <div>
29//!             <h1>{ "Secure" }</h1>
30//!             <button onclick={onclick_callback}>{ "Go Home" }</button>
31//!         </div>
32//!     }
33//! }
34//!
35//! #[function_component(Main)]
36//! fn app() -> Html {
37//!     html! {
38//!         <BrowserRouter>
39//!             <Switch<Route> render={switch} />
40//!         </BrowserRouter>
41//!     }
42//! }
43//!
44//! fn switch(routes: Route) -> Html {
45//!     match routes {
46//!         Route::Home => html! { <h1>{ "Home" }</h1> },
47//!         Route::Secure => html! {
48//!             <Secure />
49//!         },
50//!         Route::NotFound => html! { <h1>{ "404" }</h1> },
51//!     }
52//! }
53//! ```
54//!
55//! # Internals
56//!
57//! The router registers itself as a context provider and makes location information and navigator
58//! available via [`hooks`] or [`RouterScopeExt`](scope_ext::RouterScopeExt).
59//!
60//! # State
61//!
62//! The [`Location`](gloo::history::Location) API has a way to access / store state associated with
63//! session history. Please consult [`location.state()`](crate::history::Location::state) for
64//! detailed usage.
65
66extern crate self as yew_router;
67
68#[doc(hidden)]
69#[path = "macro_helpers.rs"]
70pub mod __macro;
71pub mod components;
72pub mod hooks;
73pub mod navigator;
74mod routable;
75pub mod router;
76pub mod scope_ext;
77pub mod switch;
78pub mod utils;
79
80pub use routable::{AnyRoute, Routable};
81pub use router::{BrowserRouter, HashRouter, Router};
82pub use switch::Switch;
83
84pub mod history {
85    //! A module that provides universal session history and location information.
86
87    pub use gloo::history::{
88        AnyHistory, BrowserHistory, HashHistory, History, HistoryError, HistoryResult, Location,
89        MemoryHistory,
90    };
91}
92
93pub mod prelude {
94    //! Prelude module to be imported when working with `yew-router`.
95    //!
96    //! This module re-exports the frequently used types from the crate.
97
98    pub use crate::components::{Link, Redirect};
99    pub use crate::history::Location;
100    pub use crate::hooks::*;
101    pub use crate::navigator::{NavigationError, NavigationResult, Navigator};
102    pub use crate::scope_ext::{LocationHandle, NavigatorHandle, RouterScopeExt};
103    #[doc(no_inline)]
104    pub use crate::Routable;
105    pub use crate::{BrowserRouter, HashRouter, Router, Switch};
106}