Allow redirect attribute to be parsed by the Routable macro
#2187
Madoshakalaka
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
|
This is not the only use of
For example, you can do something like: #[function_component(SomePage)]
fn some_page() -> Html {
let user = match use_user() {
Some(m) => m,
None => return html! { <AppRedirect to={AppRoute::Login} /> },
};
// ... actual page content.
}Without #[function_component(SomePage)]
fn some_page() -> Html {
let history = use_history().unwrap_or_default();
let user = match use_user() {
Some(m) => m,
None => return {
history.push(AppRoute::Login);
html! { }
},
};
// ... actual page content.
}
I think they complement each other and both have legitimate use. With that being said, I think it's good to add |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Edit: This is originally titled "Remove attribute and integrate it into the
Routablemacro. Then as this #2187 (comment) points out,<Redirect />might have its value. Thus I changed the title to the current "Allow redirect attribute to be parsed by theRoutablemacro"<Redirect/>seems less relevant after the introduction of thehistoryAPI. Currently, yew-router offers both<Redirect/>andhistory.push(), which does exactly the same things, in fact, the former uses the latter, with an extra unused empty HTML return (as the pushed history will overwrite the page)https://github.com/yewstack/yew/blob/master/packages/yew-router/src/components/redirect.rs#L15-L31
<Redirect/>is a sort of a "side effect" component that doesn't render anything by itself. if it's is used anywhere inside a component, it can be replaced withhistory.push(target_route);(or the normal component counterpart of the history API). The only place it can be legitimately used is theswitchfunction where hooks/ctx.link is not available. Since this is the only place<Redict/>might be used. We can integrate it to theRoutableAPI.Consider a general case where we have two Routers, a
MainRouteand aSettingsRoute.Here's the code with the current syntax:
Now say we want to add redirect
settings/toMainRoute::Home, this is how we currently do it:Proposal
Instead of having a
<Redirect/>in theswitchfunction. We can accept#[redirect()]attributes on theSettingsRoutestruct. Thus saving the need to have a variant and the need to include it in theswitchfunction.And multiple redirect works alike:
Selfshould be treated as a keyword in the proc macro to direct to a variant in the current Route:Not Found route
A caveat is how
not_foundshould be redirected as currently the not found route requires a variant. We can handlenot_foundas a special input to theredirectattribute. Say we want a unified Not Found route underMainRoute:Beta Was this translation helpful? Give feedback.
All reactions