-
-
Notifications
You must be signed in to change notification settings - Fork 627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Variably mounted children stop responding to signals on second mount #2488
Comments
I should add, this example when using bog standard #[component]
pub fn Child(children: Children) -> impl IntoView {
let show = create_rw_signal(false);
let children = store_value(children());
view! {
<div>
<button on:click=move |_| {
show.update(|v| *v = !*v);
}>"Toggle children"</button>
<div>{move || format!("Children shown: {}", show.get())}</div>
{move || {
show.get()
.then(move || {
view! { <div>{children.get_value()}</div> }
})
}}
</div>
}
} leads to a leptos panic and toggle btn becoming unresponsive: |
Taking a step back for a minute: Is there a reason that you actually need It is fine if so, and I'm happy to walk you through how to implement it correctly, but it's probably a waste of time unless there is some specific reason not to use those. #[component]
pub fn Child(children: ChildrenFn) -> impl IntoView {
let show = create_rw_signal(false);
view! {
<div>
<button on:click=move |_| {
show.update(|v| *v = !*v);
}>"Toggle children"</button>
<div>{move || format!("Children shown: {}", show.get())}</div>
<Show when=show children/>
</div>
}
} |
Hmmm, I haven't been using I'll do some refactoring (the place I found this bug has 4 chained |
It's not super complicated, but complicated enough. Basically by lazily constructing the children inside a reactive I guess there's a simple enough solution: Just take your example #[component]
pub fn Child(children: Children) -> impl IntoView {
let show = create_rw_signal(false);
let owner = Owner::current().unwrap();
let children = store_value(once_cell::unsync::Lazy::new(move || {
with_owner(owner, children)
}));
view! { |
You're the best! Makes perfect sense and seems to work as described. Fyi, I checked and My /// Like [`leptos::store_value`], but will only call the getter if needed.
pub fn store_value_lazy<T>(value_getter: impl FnOnce() -> T + 'static) -> StoredValueLazy<T> {
StoredValueLazy::new(value_getter)
}
#[derive(Debug)]
pub struct StoredValueLazy<T: 'static> {
value: StoredValue<once_cell::unsync::Lazy<T, Box<dyn FnOnce() -> T>>>,
}
impl<T> Clone for StoredValueLazy<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Copy for StoredValueLazy<T> {}
impl<T> StoredValueLazy<T> {
pub fn new(value_getter: impl FnOnce() -> T + 'static) -> Self {
let owner = Owner::current().unwrap();
Self {
value: store_value(once_cell::unsync::Lazy::new(Box::new(move || {
with_owner(owner, value_getter)
}))),
}
}
#[track_caller]
pub fn with_value<R>(&self, cb: impl FnOnce(&T) -> R) -> R {
self.value.with_value(|value| cb(value))
}
}
impl<T: Clone> StoredValueLazy<T> {
#[track_caller]
pub fn get_value(&self) -> T {
self.with_value(|v| v.clone())
}
} Would it be worth PR'ing in and modifying |
No, I'll close this one if you're happy with the solution! |
Got it, thanks for your help! |
Describe the bug
Continuation of #2482, but sort of different so opening new, turns out the solution provided is still buggy. If children are unmounted, any reactive blocks stop updating on remount.
Screen.Recording.2024-04-02.at.17.39.26.mp4
Leptos Dependencies
For example:
To Reproduce
<Parent />
Expected behavior
Children shouldn't lose reactivity after the first mount/unmount.
Completely appreciate and excited for the work being done for 0.7, if this is only going to be properly fixed then, any ideas how to get something working for now?
The text was updated successfully, but these errors were encountered: