Suspense can't detect the data changes in the Store. #3602
-
According to this document https://book.leptos.dev/view/04b_iteration.html, I put the dynamically returned data into the Store. However, in the Suspense, it seems that the modification of the data in the Store cannot be monitored. The data in the Store is only executed once when the page is loaded. Although the data in the Store has been modified during subsequent interactions, the page display does not change. #[island]
fn UserRow(id: u64, name: String) -> impl IntoView {
let checked = RwSignal::new(false);
let selected_user = expect_context::<RwSignal<u64>>();
let on_click = move |_| {
checked(!checked());
if checked() {
leptos::logging::log!("check:{id}");
selected_user(id);
} else {
leptos::logging::log!("cancel check");
selected_user(0);
}
};
view! {
<Checkbox label=name value=id.to_string() on:click=on_click />
}
}
#[island]
fn UserController() -> impl IntoView {
let users = vec![];
let users_store = Store::new(Users { users });
let selected_user = RwSignal::new(0u64);
provide_context(selected_user);
let add_user_action = ServerAction::<AddUser>::new();
let del_user_action = ServerAction::<DelUser>::new();
let update_user_list_action = leptos::prelude::Action::new(move |input: &String| {
let _ = input.to_owned();
async move {
match list_user().await {
Ok(users_resp) => {
if users_resp.is_empty() {
users_store.users().set(vec![]);
} else {
users_store.users().set(users_resp.iter().map(|u| u.to_store()).collect());
}
},
Err(e) => {
leptos::logging::log!("{e}");
users_store.users().set(vec![]);
},
};
}
});
let list_view = move || Suspend::new(async move {
let uses_list = &users_store.read_untracked().users;
leptos::logging::log!("{uses_list:#?}");
if users_store.read_untracked().users.len() == 0 {
Either::Left(view! {<p>not data</p>})
} else {
Either::Right(view! {
<For
each=move || users_store.users()
key=|row| row.read().id.clone()
children=move |user| {
view! { <UserRow name=user.name().get() id=user.id().get() /> }
}
/>
})
}
});
Effect::watch(
move || (
del_user_action.version()(),
add_user_action.version()(),
),
move |curr, perv, _| {
update_user_list_action.dispatch("".to_string());
},
true,
);
let on_add_user = move |_| {
add_user_action.dispatch(AddUser { });
};
let on_list_user = move |_| {
update_user_list_action.dispatch("".to_string());
};
let on_del_user = move |_| {
del_user_action.dispatch(DelUser { user_id: selected_user() });
};
view! {
<Button loading=add_user_action.pending() on:click=on_add_user>add user</Button>
<Button loading=update_user_list_action.pending() on:click=on_list_user>list user</Button>
<Button loading=del_user_action.pending() on:click=on_del_user>del user</Button>
<Suspense
fallback=move || view! { <p>"Loading..."</p> }
>
{list_view}
</Suspense>
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I don't see a reason to use One reason the UI is not updating when the store data changes could be that you are explicitly untracking it: let uses_list = &users_store.read_untracked().users;
// ...
if users_store.read_untracked().users.len() == 0 { |
Beta Was this translation helpful? Give feedback.
I don't see a reason to use
Suspense
orSuspend
in this example:Suspense
is used for reading data from resources, which you are not doing.One reason the UI is not updating when the store data changes could be that you are explicitly untracking it: