Skip to content

Commit

Permalink
Add a warning when Link it called outside of a Router context
Browse files Browse the repository at this point in the history
  • Loading branch information
autarch committed Jan 14, 2022
1 parent 9b282d8 commit d562241
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions packages/router/src/components/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,21 @@ pub struct LinkProps<'a> {
}

pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
let service = cx.consume_context::<RouterService>()?;
cx.render(rsx! {
a {
href: "{cx.props.to}",
class: format_args!("{}", cx.props.class.unwrap_or("")),
id: format_args!("{}", cx.props.id.unwrap_or("")),
title: format_args!("{}", cx.props.title.unwrap_or("")),

prevent_default: "onclick",
onclick: move |_| service.push_route(cx.props.to),

&cx.props.children
}
})
if let Some(service) = cx.consume_context::<RouterService>() {
return cx.render(rsx! {
a {
href: "{cx.props.to}",
class: format_args!("{}", cx.props.class.unwrap_or("")),
id: format_args!("{}", cx.props.id.unwrap_or("")),
title: format_args!("{}", cx.props.title.unwrap_or("")),

prevent_default: "onclick",
onclick: move |_| service.push_route(cx.props.to),

&cx.props.children
}
});
}
log::warn!("Attempted to create a Link to {} outside of a Router context", cx.props.to);
None
}

0 comments on commit d562241

Please sign in to comment.