fix external router examples

This commit is contained in:
Evan Almloff committed 2023-06-01 14:10:33 -05:00
1 parent 45a004bf74
commit 07446386e6
7 files changed
+157 -133

No files matched your search

@@ -8,7 +8,7 @@
#![allow(non_snake_case)]
use dioxus::prelude::*;
use dioxus_fullstack::prelude::*;
use dioxus_router::*;
use dioxus_router::prelude::*;
use serde::{Deserialize, Serialize};
fn main() {
@@ -34,6 +34,8 @@ fn main() {
}));
use axum::extract::State;
use std::str::FromStr;
PostServerData::register().unwrap();
GetServerData::register().unwrap();
tokio::runtime::Runtime::new()
@@ -57,7 +59,7 @@ fn main() {
&ServeConfigBuilder::new(
App,
AppProps {
route: Some(format!("http://{addr}{uri}")),
route: Route::from_str(&uri.to_string()).ok(),
},
)
.build(),
@@ -74,48 +76,64 @@ fn main() {
}
}
#[derive(Clone, Debug, Props, PartialEq, Serialize, Deserialize)]
struct AppProps {
route: Option<String>,
#[derive(Clone, Routable, Serialize, Deserialize, Debug, PartialEq)]
enum Route {
#[route("/")]
Home {},
#[route("/blog")]
Blog {},
}
fn App(cx: Scope<AppProps>) -> Element {
cx.render(rsx! {
Router {
initial_url: cx.props.route.clone(),
Route { to: "/blog",
Link {
to: "/",
"Go to counter"
}
table {
tbody {
#[inline_props]
fn Blog(cx: Scope) -> Element {
render! {
Link {
target: Route::Home {},
"Go to counter"
}
table {
tbody {
for _ in 0..100 {
tr {
for _ in 0..100 {
tr {
for _ in 0..100 {
td { "hello world!" }
}
}
td { "hello world!" }
}
}
}
},
// Fallback
Route { to: "",
Counter {}
},
}
}
}
}
#[derive(Clone, Debug, Props, PartialEq, Serialize, Deserialize)]
struct AppProps {
route: Option<Route>,
}
fn App(cx: Scope<AppProps>) -> Element {
#[cfg(feature = "ssr")]
let initial_route = cx.props.route.clone().unwrap_or(Route::Home {});
cx.render(rsx! {
Router {
config: move || RouterConfig::default().history({
#[cfg(feature = "ssr")]
let history = MemoryHistory::with_initial_path(initial_route);
#[cfg(feature = "web")]
let history = WebHistory::default();
history
})
}
})
}
fn Counter(cx: Scope) -> Element {
#[inline_props]
fn Home(cx: Scope) -> Element {
let mut count = use_state(cx, || 0);
let text = use_state(cx, || "...".to_string());
cx.render(rsx! {
Link {
to: "/blog",
target: Route::Blog {}
"Go to blog"
}
div{
+4 -6
View File
@@ -5,13 +5,13 @@ use std::{cell::RefCell, str::FromStr};
use crate::{
prelude::{GenericOutlet, GenericRouterContext},
routable::Routable,
router_cfg::RouterConfiguration,
router_cfg::RouterConfig,
};
/// The config for [`GenericRouter`].
pub struct RouterConfigFactory<R: Routable> {
#[allow(clippy::type_complexity)]
config: RefCell<Option<Box<dyn FnOnce() -> RouterConfiguration<R>>>>,
config: RefCell<Option<Box<dyn FnOnce() -> RouterConfig<R>>>>,
}
impl<R: Routable> Default for RouterConfigFactory<R>
@@ -19,13 +19,11 @@ where
<R as FromStr>::Err: std::fmt::Display,
{
fn default() -> Self {
Self::from(RouterConfiguration::default)
Self::from(RouterConfig::default)
}
}
impl<R: Routable, F: FnOnce() -> RouterConfiguration<R> + 'static> From<F>
for RouterConfigFactory<R>
{
impl<R: Routable, F: FnOnce() -> RouterConfig<R> + 'static> From<F> for RouterConfigFactory<R> {
fn from(value: F) -> Self {
Self {
config: RefCell::new(Some(Box::new(value))),
+4 -6
View File
@@ -7,7 +7,7 @@ use dioxus::prelude::*;
use crate::{
history::HistoryProvider, navigation::NavigationTarget, routable::Routable,
router_cfg::RouterConfiguration,
router_cfg::RouterConfig,
};
/// An error that can occur when navigating.
@@ -15,7 +15,8 @@ use crate::{
pub struct ExternalNavigationFailure(String);
/// A function the router will call after every routing update.
pub(crate) type RoutingCallback<R> = Arc<dyn Fn(GenericRouterContext<R>) -> Option<NavigationTarget<R>>>;
pub(crate) type RoutingCallback<R> =
Arc<dyn Fn(GenericRouterContext<R>) -> Option<NavigationTarget<R>>>;
struct MutableRouterState<R>
where
@@ -70,10 +71,7 @@ impl<R> GenericRouterContext<R>
where
R: Routable,
{
pub(crate) fn new(
cfg: RouterConfiguration<R>,
mark_dirty: Arc<dyn Fn(ScopeId) + Sync + Send>,
) -> Self
pub(crate) fn new(cfg: RouterConfig<R>, mark_dirty: Arc<dyn Fn(ScopeId) + Sync + Send>) -> Self
where
R: Clone,
{
+5 -7
View File
@@ -33,17 +33,15 @@ where
/// OtherPage {},
/// }
///
/// let mut history = MemoryHistory::<Route>::with_initial_path("/").unwrap();
/// let mut history = MemoryHistory::<Route>::with_initial_path(Route::Index {}).unwrap();
/// assert_eq!(history.current_route(), Route::Index {});
/// assert_eq!(history.can_go_back(), false);
/// ```
pub fn with_initial_path(path: impl AsRef<str>) -> Result<Self, <R as FromStr>::Err> {
let path = path.as_ref();
Ok(Self {
current: R::from_str(path)?,
pub fn with_initial_path(path: R) -> Self {
Self {
current: path,
..Default::default()
})
}
}
}
+1 -1
View File
@@ -56,7 +56,7 @@ pub mod prelude {
pub use crate::hooks::*;
pub use crate::navigation::*;
pub use crate::routable::*;
pub use crate::router_cfg::RouterConfiguration;
pub use crate::router_cfg::RouterConfig;
pub use dioxus_router_macro::Routable;
#[doc(hidden)]
+4 -4
View File
@@ -23,15 +23,15 @@ use crate::prelude::*;
/// #[route("/")]
/// Index {},
/// }
/// let cfg = RouterConfiguration::default().history(WebHistory<Route>::default());
/// let cfg = RouterConfig::default().history(WebHistory<Route>::default());
/// ```
pub struct RouterConfiguration<R: Routable> {
pub struct RouterConfig<R: Routable> {
pub(crate) failure_external_navigation: fn(Scope) -> Element,
pub(crate) history: Box<dyn HistoryProvider<R>>,
pub(crate) on_update: Option<RoutingCallback<R>>,
}
impl<R: Routable + Clone> Default for RouterConfiguration<R>
impl<R: Routable + Clone> Default for RouterConfig<R>
where
<R as std::str::FromStr>::Err: std::fmt::Display,
{
@@ -50,7 +50,7 @@ where
}
}
impl<R: Routable> RouterConfiguration<R> {
impl<R: Routable> RouterConfig<R> {
/// A function to be called whenever the routing is updated.
///
/// The callback is invoked after the routing is updated, but before components and hooks are