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

yew_router/components/
redirect.rs

1use wasm_bindgen::UnwrapThrowExt;
2use yew::prelude::*;
3
4use crate::hooks::use_navigator;
5use crate::Routable;
6
7/// Props for [`Redirect`]
8#[derive(Properties, Clone, PartialEq, Eq)]
9pub struct RedirectProps<R: Routable> {
10    /// Route that will be pushed when the component is rendered.
11    pub to: R,
12}
13
14/// A component that will redirect to specified route when rendered.
15#[function_component(Redirect)]
16pub fn redirect<R>(props: &RedirectProps<R>) -> Html
17where
18    R: Routable + 'static,
19{
20    let history = use_navigator().expect_throw("failed to read history.");
21
22    let target_route = props.to.clone();
23    use_effect(move || {
24        history.push(&target_route);
25
26        || {}
27    });
28
29    Html::default()
30}