-
-
Notifications
You must be signed in to change notification settings - Fork 589
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
Make Show
component behave in a more intuitive way
#2261
Comments
Thanks! Copying and pasting my reply from Discord so that it is visible here/I remember what I was talking about if I look at this in the future. The explanation here is fairly simple, although it might not be satisfactory. Because of the nature of fine grained reactive rendering, the Show component there creates two effects that both read from selected_item:
Updating selected_item triggers both of these, which depend on it. It is not necessarily the case that they run in the order you’re expecting, ie that the “show the fallback” effect runs first and cancels the “update a text node” effect by unmounting it.
|
Note: I cannot actually reproduce the issue you're describing with the example above. Here's my attempt at a reproduction, which does not seem to panic at the #[component]
pub fn App() -> impl IntoView {
let (selected_item, set_selected_item) = create_signal(Some(1));
view! {
<button on:click=move |_| set_selected_item(None)>"None"</button>
<button on:click=move |_| set_selected_item(Some(2))>"Some(_)"</button>
<SelectionView selected_item />
}
}
#[component]
fn SelectionView(selected_item: ReadSignal<Option<usize>>) -> impl IntoView {
view! {
<Show
when=move || selected_item.get().is_some()
fallback=|| {
view! { "No item selected" }
}
>
"Item "
{move || selected_item.get().unwrap()}
" is selected"
</Show>
}
} |
Here is a repo with instructions on how to trigger the panic: https://github.com/kcprs/leptos-show-problem |
I'm happy to say: you win the "issue of the week" award, for turning up a very small but significant flaw in the implementation of the reactive system, and making life better for all users! 🎉 I've seen variations on this pop up occasionally for a while, but hadn't really looked at the root cause. |
fix: guarantee execution order of effects (closes #2261)
We should have an awesome-issues like awesome-leptos lol |
Is your feature request related to a problem? Please describe.
The
Show
component behaves in a way that is non-intuitive, especially to people who are new to fine-grained reactivity. For example, the call tounwrap()
below seems safe because one would expect the children components to be rendered only if thewhen
condition istrue
. In reality, the order in which effects react to the change of theselected_item
signal is not deterministic, leading to spurious panics.Describe the solution you'd like
Perhaps it would be possible to ensure that effects run in the intuitive order.
The text was updated successfully, but these errors were encountered: