Added initial_url to the router component (#510)

* added active_url

* renamed active_url to initial_url

* updated docs
This commit is contained in:
Norman Paniagua authored and GitHub committed 2022-09-13 13:09:00 -07:00
1 parent 67dc6e6017
commit 6e66d6fe36
4 files changed
+34 -8

No files matched your search

+1
View File
@@ -2,4 +2,5 @@
pub struct RouterCfg {
pub base_url: Option<String>,
pub active_class: Option<String>,
pub initial_url: Option<String>,
}
+4
View File
@@ -28,6 +28,9 @@ pub struct RouterProps<'a> {
/// This is useful if you don't want to repeat the same `active_class` prop value in every Link.
/// By default set to `"active"`.
pub active_class: Option<&'a str>,
/// Set the initial url.
pub initial_url: Option<String>,
}
/// A component that conditionally renders children based on the current location of the app.
@@ -43,6 +46,7 @@ pub fn Router<'a>(cx: Scope<'a, RouterProps<'a>>) -> Element {
RouterCfg {
base_url: cx.props.base_url.map(|s| s.to_string()),
active_class: cx.props.active_class.map(|s| s.to_string()),
initial_url: cx.props.initial_url.clone(),
},
))
});
+14 -1
View File
@@ -9,6 +9,7 @@ use std::{
cell::{Cell, RefCell},
collections::{HashMap, HashSet},
rc::Rc,
str::FromStr,
sync::Arc,
};
use url::Url;
@@ -84,7 +85,19 @@ impl RouterCore {
#[cfg(not(feature = "web"))]
let history = Box::new(hash::new());
let route = Arc::new(history.init_location());
let route = match &cfg.initial_url {
Some(url) => Arc::new(ParsedRoute {
url: Url::from_str(&url).expect(
format!(
"RouterCfg expects a valid initial_url, but got '{}'. Example: '{{scheme}}://{{?authority}}/{{?path}}'",
&url
).as_str()
),
title: None,
serialized_state: None,
}),
None => Arc::new(history.init_location()),
};
let svc = Arc::new(Self {
cfg,