fix outlets being updated out of order

This commit is contained in:
Evan Almloff committed 2023-05-23 12:59:31 -05:00
1 parent f94dc85491
commit 46017d5b7e
3 files changed
+36 -10

No files matched your search

+4 -2
View File
@@ -297,11 +297,13 @@ impl RouteEnum {
#index_iter => { #index_iter => {
match myself { match myself {
#layers #layers
_ => panic!("Route::render called with invalid level {}", level), // _ => panic!("Route::render called with invalid level {}", level),
_ => None
} }
}, },
)* )*
_ => panic!("Route::render called with invalid level {}", level), // _ => panic!("Route::render called with invalid level {}", level),
_ => None
} }
} }
} }
+28 -7
View File
@@ -3,18 +3,39 @@ use log::error;
use std::{cell::RefCell, str::FromStr}; use std::{cell::RefCell, str::FromStr};
use crate::{ use crate::{
prelude::{outlet::OutletContext, RouterContext}, prelude::{Outlet, RouterContext},
routable::Routable, routable::Routable,
router_cfg::RouterConfiguration, router_cfg::RouterConfiguration,
}; };
/// The config for [`Router`].
pub struct RouterCfg<R: Routable> {
config: RefCell<Option<RouterConfiguration<R>>>,
}
impl<R: Routable> Default for RouterCfg<R> {
fn default() -> Self {
Self {
config: RefCell::new(Some(RouterConfiguration::default())),
}
}
}
impl<R: Routable> From<RouterConfiguration<R>> for RouterCfg<R> {
fn from(value: RouterConfiguration<R>) -> Self {
Self {
config: RefCell::new(Some(value)),
}
}
}
/// The props for [`Router`]. /// The props for [`Router`].
#[derive(Props)] #[derive(Props)]
pub struct RouterProps<R: Routable> { pub struct RouterProps<R: Routable> {
#[props(into)] #[props(into)]
initial_url: Option<String>, initial_url: Option<String>,
#[props(default, into)] #[props(default, into)]
config: RefCell<Option<RouterConfiguration<R>>>, config: RouterCfg<R>,
} }
impl<R: Routable> PartialEq for RouterProps<R> { impl<R: Routable> PartialEq for RouterProps<R> {
@@ -29,7 +50,7 @@ pub fn Router<R: Routable + Clone>(cx: Scope<RouterProps<R>>) -> Element
where where
<R as FromStr>::Err: std::fmt::Display, <R as FromStr>::Err: std::fmt::Display,
{ {
let router = use_context_provider(cx, || { use_context_provider(cx, || {
#[allow(unreachable_code, unused_variables)] #[allow(unreachable_code, unused_variables)]
if let Some(outer) = cx.consume_context::<RouterContext<R>>() { if let Some(outer) = cx.consume_context::<RouterContext<R>>() {
let msg = "Router components should not be nested within each other"; let msg = "Router components should not be nested within each other";
@@ -38,7 +59,7 @@ where
panic!("{}", msg); panic!("{}", msg);
} }
let router = RouterContext::new( let router = RouterContext::new(
cx.props.config.take().unwrap_or_default(), cx.props.config.config.take().unwrap_or_default(),
cx.schedule_update_any(), cx.schedule_update_any(),
); );
if let Some(initial) = cx.props.initial_url.as_ref() { if let Some(initial) = cx.props.initial_url.as_ref() {
@@ -51,7 +72,7 @@ where
router router
}); });
use_context_provider(cx, || OutletContext { current_level: 1 }); render! {
Outlet::<R> {}
router.current().render(cx, 0) }
} }
+4 -1
View File
@@ -8,7 +8,10 @@ pub(crate) struct OutletContext {
} }
pub(crate) fn use_outlet_context(cx: &ScopeState) -> &OutletContext { pub(crate) fn use_outlet_context(cx: &ScopeState) -> &OutletContext {
let outlet_context = use_context(cx).unwrap(); let outlet_context = cx.use_hook(|| {
cx.consume_context()
.unwrap_or(OutletContext { current_level: 0 })
});
outlet_context outlet_context
} }