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
-
Edit: This is originally titled "Remove attribute and integrate it into the
Routable
macro. 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 theRoutable
macro"<Redirect/>
seems less relevant after the introduction of thehistory
API. 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 theswitch
function where hooks/ctx.link is not available. Since this is the only place<Redict/>
might be used. We can integrate it to theRoutable
API.Consider a general case where we have two Routers, a
MainRoute
and 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 theswitch
function. We can accept#[redirect()]
attributes on theSettingsRoute
struct. Thus saving the need to have a variant and the need to include it in theswitch
function.And multiple redirect works alike:
Self
should 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_found
should be redirected as currently the not found route requires a variant. We can handlenot_found
as a special input to theredirect
attribute. Say we want a unified Not Found route underMainRoute
:Beta Was this translation helpful? Give feedback.
All reactions