Add a warning when Link it called outside of a Router context

This commit is contained in:
Dave Rolsky committed 2022-01-17 15:52:01 -06:00
1 parent 9b282d877b
commit 64080588d0
1 file changed
+19 -12
+19 -12
View File
@@ -41,18 +41,25 @@ pub struct LinkProps<'a> {
} }
pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element { pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
let service = cx.consume_context::<RouterService>()?; log::debug!("render Link to {}", cx.props.to);
cx.render(rsx! { if let Some(service) = cx.consume_context::<RouterService>() {
a { return cx.render(rsx! {
href: "{cx.props.to}", a {
class: format_args!("{}", cx.props.class.unwrap_or("")), href: "{cx.props.to}",
id: format_args!("{}", cx.props.id.unwrap_or("")), class: format_args!("{}", cx.props.class.unwrap_or("")),
title: format_args!("{}", cx.props.title.unwrap_or("")), id: format_args!("{}", cx.props.id.unwrap_or("")),
title: format_args!("{}", cx.props.title.unwrap_or("")),
prevent_default: "onclick", prevent_default: "onclick",
onclick: move |_| service.push_route(cx.props.to), onclick: move |_| service.push_route(cx.props.to),
&cx.props.children &cx.props.children
} }
}) });
}
log::warn!(
"Attempted to create a Link to {} outside of a Router context",
cx.props.to,
);
None
} }