Conditional updates #2228
-
Does leptos support conditional updates of signals for instance if the update function returns an Err. I know that you can wrap the signal in a Result, but that doesn't work for my use case I was thinking something along the lines of this (the example is thought up, but my use case is also game history related, where I can't use a #[component]
fn MyComponent() -> impl IntoView {
let game_history = RwSignal::new(GameHistory::new());
view! {
<button on:click=move |_| {
game_history.update(|history| {
if let Err(e) = history.purchase_artifact() {
// if this is an Err, say the player doesn't have enough money, then don't update the game_history signal
show_error(e);
}
});
}>
"Update history"
</button>
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This depends on the signature of match game_history.with(GameHistory::purchase_artifact) {
Err(e) => show_error(e),
Ok(value) => game_history.update(/* ... */)
} If it takes |
Beta Was this translation helpful? Give feedback.
This depends on the signature of
purchase_artifact
. If it takes&self
then you can do a conditional update with something like:If it takes
&mut
, then there is not a way to do this without the framework adding some additionalPartialEq
constraint or something, or creating a special helper method that doesn't currently exist.