yew_nested_router/
switch.rs

1use crate::router::use_router;
2use crate::target::Target;
3use std::fmt::Debug;
4use yew::prelude::*;
5
6#[derive(Clone, Debug, PartialEq, Properties)]
7pub struct SwitchProps<T>
8where
9    T: Target,
10{
11    /// The function rendering based on the active target.
12    pub render: Callback<T, Html>,
13
14    /// The default, in case no route is active (not found).
15    #[prop_or_default]
16    pub default: Html,
17}
18
19/// A component two switch rendering between the different targets.
20#[function_component(Switch)]
21pub fn switch<T>(props: &SwitchProps<T>) -> Html
22where
23    T: Target + 'static,
24{
25    let router = use_router::<T>().expect("Must be a child of a Router or Nested component");
26
27    match router.active_target {
28        Some(target) => props.render.emit(target),
29        None => props.default.clone(),
30    }
31}