1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use wasm_bindgen::UnwrapThrowExt;
use yew::prelude::*;

use crate::hooks::use_navigator;
use crate::Routable;

/// Props for [`Redirect`]
#[derive(Properties, Clone, PartialEq, Eq)]
pub struct RedirectProps<R: Routable> {
    /// Route that will be pushed when the component is rendered.
    pub to: R,
}

/// A component that will redirect to specified route when rendered.
#[function_component(Redirect)]
pub fn redirect<R>(props: &RedirectProps<R>) -> Html
where
    R: Routable + 'static,
{
    let history = use_navigator().expect_throw("failed to read history.");

    let target_route = props.to.clone();
    use_effect(move || {
        history.push(&target_route);

        || {}
    });

    Html::default()
}