diff --git a/leptos_reactive/src/stored_value.rs b/leptos_reactive/src/stored_value.rs index 538770732..b70aa2429 100644 --- a/leptos_reactive/src/stored_value.rs +++ b/leptos_reactive/src/stored_value.rs @@ -194,8 +194,10 @@ impl StoredValue { /// stored value has not yet been disposed, [`None`] otherwise. pub fn try_update_value(self, f: impl FnOnce(&mut T) -> O) -> Option { with_runtime(self.runtime, |runtime| { - let values = runtime.stored_values.borrow(); - let value = values.get(self.id)?; + let value = { + let values = runtime.stored_values.borrow(); + values.get(self.id)?.clone() + }; let mut value = value.borrow_mut(); let value = value.downcast_mut::()?; Some(f(value)) @@ -228,13 +230,20 @@ impl StoredValue { /// stored value has not yet been disposed, [`Some(T)`] otherwise. pub fn try_set_value(&self, value: T) -> Option { with_runtime(self.runtime, |runtime| { - let values = runtime.stored_values.borrow(); - let n = values.get(self.id); - let mut n = n.map(|n| n.borrow_mut()); - let n = n.as_mut().and_then(|n| n.downcast_mut::()); + let n = { + let values = runtime.stored_values.borrow(); + values.get(self.id).map(Rc::clone) + }; + if let Some(n) = n { - *n = value; - None + let mut n = n.borrow_mut(); + let n = n.downcast_mut::(); + if let Some(n) = n { + *n = value; + None + } else { + Some(value) + } } else { Some(value) }