pub struct StorageEntry<S: StorageBacking, T: Serialize + DeserializeOwned + Clone + Send + Sync + 'static> { /* private fields */ }Expand description
A storage entry that can be used to store data across application reloads. It optionally provides a channel to subscribe to updates to the underlying storage.
Implementations§
Source§impl<S, T> StorageEntry<S, T>where
S: StorageBacking,
T: Serialize + DeserializeOwned + Clone + Send + Sync + 'static,
S::Key: Clone,
impl<S, T> StorageEntry<S, T>where
S: StorageBacking,
T: Serialize + DeserializeOwned + Clone + Send + Sync + 'static,
S::Key: Clone,
Methods from Deref<Target = Signal<T>>§
Sourcepub fn point_to(&self, other: Signal<T, S>) -> Result<(), BorrowError>where
T: 'static,
pub fn point_to(&self, other: Signal<T, S>) -> Result<(), BorrowError>where
T: 'static,
Point to another signal. This will subscribe the other signal to all subscribers of this signal.
Sourcepub fn manually_drop(&self)where
T: 'static,
pub fn manually_drop(&self)where
T: 'static,
Drop the value out of the signal, invalidating the signal in the process.
Sourcepub fn origin_scope(&self) -> ScopeId
pub fn origin_scope(&self) -> ScopeId
Get the scope the signal was created in.
Sourcepub fn id(&self) -> GenerationalBoxId
pub fn id(&self) -> GenerationalBoxId
Get the generational id of the signal.
Sourcepub fn write_silent(&self) -> WriteLock<'static, T, S>
👎Deprecated: This pattern is no longer recommended. Prefer peek or creating new signals instead.
pub fn write_silent(&self) -> WriteLock<'static, T, S>
peek or creating new signals instead.This pattern is no longer recommended. Prefer peek or creating new signals instead.
This function is the equivalent of the write_silent method on use_ref.
§What you should use instead
§Reading and Writing to data in the same scope
Reading and writing to the same signal in the same scope will cause that scope to rerun forever:
let mut signal = use_signal(|| 0);
// This makes the scope rerun whenever we write to the signal
println!("{}", *signal.read());
// This will rerun the scope because we read the signal earlier in the same scope
*signal.write() += 1;You may have used the write_silent method to avoid this infinite loop with use_ref like this:
let signal = use_signal(|| 0);
// This makes the scope rerun whenever we write to the signal
println!("{}", *signal.read());
// Write silent will not rerun any subscribers
*signal.write_silent() += 1;Instead you can use the peek and write methods instead. The peek method will not subscribe to the current scope which will avoid an infinite loop if you are reading and writing to the same signal in the same scope.
let mut signal = use_signal(|| 0);
// Peek will read the value but not subscribe to the current scope
println!("{}", *signal.peek());
// Write will update any subscribers which does not include the current scope
*signal.write() += 1;§Reading and Writing to different data
§Why is this pattern no longer recommended?
This pattern is no longer recommended because it is very easy to allow your state and UI to grow out of sync. write_silent globally opts out of automatic state updates which can be difficult to reason about.
Lets take a look at an example: main.rs:
fn app() -> Element {
let signal = use_context_provider(|| Signal::new(0));
// We want to log the value of the signal whenever the app component reruns
println!("{}", *signal.read());
rsx! {
button {
// If we don't want to rerun the app component when the button is clicked, we can use write_silent
onclick: move |_| *signal.write_silent() += 1,
"Increment"
}
Child {}
}
}child.rs:
fn Child() -> Element {
let signal: Signal<i32> = use_context();
// It is difficult to tell that changing the button to use write_silent in the main.rs file will cause UI to be out of sync in a completely different file
rsx! {
"{signal}"
}
}Instead peek locally opts out of automatic state updates explicitly for a specific read which is easier to reason about.
Here is the same example using peek: main.rs:
fn app() -> Element {
let mut signal = use_context_provider(|| Signal::new(0));
// We want to log the value of the signal whenever the app component reruns, but we don't want to rerun the app component when the signal is updated so we use peek instead of read
println!("{}", *signal.peek());
rsx! {
button {
// We can use write like normal and update the child component automatically
onclick: move |_| *signal.write() += 1,
"Increment"
}
Child {}
}
}child.rs:
fn Child() -> Element {
let signal: Signal<i32> = use_context();
rsx! {
"{signal}"
}
}Trait Implementations§
Source§impl<S: Clone + StorageBacking, T: Clone + Serialize + DeserializeOwned + Clone + Send + Sync + 'static> Clone for StorageEntry<S, T>
impl<S: Clone + StorageBacking, T: Clone + Serialize + DeserializeOwned + Clone + Send + Sync + 'static> Clone for StorageEntry<S, T>
Source§fn clone(&self) -> StorageEntry<S, T>
fn clone(&self) -> StorageEntry<S, T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more