mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-10 06:44:17 +00:00
Proper <Outlet/>
logic so we only rerender if it's actually a different parameter
This commit is contained in:
parent
4a426be6fb
commit
a051b1e08c
1 changed files with 24 additions and 8 deletions
|
@ -1,18 +1,34 @@
|
||||||
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
use crate::use_route;
|
use crate::use_route;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
|
|
||||||
/// Displays the child route nested in a parent route, allowing you to control exactly where
|
/// Displays the child route nested in a parent route, allowing you to control exactly where
|
||||||
/// that child route is displayed. Renders nothing if there is no nested child.
|
/// that child route is displayed. Renders nothing if there is no nested child.
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn Outlet(cx: Scope) -> Child {
|
pub fn Outlet(cx: Scope) -> Child {
|
||||||
let route = use_route(cx);
|
let route = use_route(cx);
|
||||||
(move || {
|
let is_showing = Rc::new(RefCell::new(None));
|
||||||
cx.untrack(|| {
|
let (outlet, set_outlet) = create_signal(cx, None);
|
||||||
route.child().map(|child| {
|
create_effect(cx, move |_| {
|
||||||
|
let is_showing_val = { is_showing.borrow().clone() };
|
||||||
|
let child = route.child();
|
||||||
|
match (route.child(), &is_showing_val) {
|
||||||
|
(None, _) => {
|
||||||
|
set_outlet.set(None);
|
||||||
|
}
|
||||||
|
(Some(child), Some(path))
|
||||||
|
if Some(child.original_path().to_string()) == is_showing_val =>
|
||||||
|
{
|
||||||
|
// do nothing: we don't need to rerender the component, because it's the same
|
||||||
|
}
|
||||||
|
(Some(child), _) => {
|
||||||
|
*is_showing.borrow_mut() = Some(child.original_path().to_string());
|
||||||
provide_context(child.cx(), child.clone());
|
provide_context(child.cx(), child.clone());
|
||||||
child.outlet()
|
set_outlet.set(Some(child.outlet().into_child(cx)))
|
||||||
})
|
}
|
||||||
})
|
}
|
||||||
})
|
});
|
||||||
.into_child(cx)
|
(move || outlet.get()).into_child(cx)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue