1
0

hooks_composed.rs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #![allow(unused)]
  2. use dioxus::prelude::*;
  3. fn main() {}
  4. struct AppSettings {}
  5. // ANCHOR: wrap_context
  6. fn use_settings(cx: &ScopeState) -> &UseSharedState<AppSettings> {
  7. use_shared_state::<AppSettings>(cx).expect("App settings not provided")
  8. }
  9. // ANCHOR_END: wrap_context
  10. // ANCHOR: use_storage
  11. use gloo_storage::{LocalStorage, Storage};
  12. use serde::{de::DeserializeOwned, Serialize};
  13. /// A persistent storage hook that can be used to store data across application reloads.
  14. #[allow(clippy::needless_return)]
  15. pub fn use_persistent<T: Serialize + DeserializeOwned + Default + 'static>(
  16. cx: &ScopeState,
  17. // A unique key for the storage entry
  18. key: impl ToString,
  19. // A function that returns the initial value if the storage entry is empty
  20. init: impl FnOnce() -> T,
  21. ) -> &UsePersistent<T> {
  22. // Use the use_ref hook to create a mutable state for the storage entry
  23. let state = use_ref(cx, move || {
  24. // This closure will run when the hook is created
  25. let key = key.to_string();
  26. let value = LocalStorage::get(key.as_str()).ok().unwrap_or_else(init);
  27. StorageEntry { key, value }
  28. });
  29. // Wrap the state in a new struct with a custom API
  30. // Note: We use use_hook here so that this hook is easier to use in closures in the rsx. Any values with the same lifetime as the ScopeState can be used in the closure without cloning.
  31. cx.use_hook(|| UsePersistent {
  32. inner: state.clone(),
  33. })
  34. }
  35. struct StorageEntry<T> {
  36. key: String,
  37. value: T,
  38. }
  39. /// Storage that persists across application reloads
  40. pub struct UsePersistent<T: 'static> {
  41. inner: UseRef<StorageEntry<T>>,
  42. }
  43. impl<T: Serialize + DeserializeOwned + Clone + 'static> UsePersistent<T> {
  44. /// Returns a reference to the value
  45. pub fn get(&self) -> T {
  46. self.inner.read().value.clone()
  47. }
  48. /// Sets the value
  49. pub fn set(&self, value: T) {
  50. let mut inner = self.inner.write();
  51. // Write the new value to local storage
  52. LocalStorage::set(inner.key.as_str(), &value);
  53. inner.value = value;
  54. }
  55. }
  56. // ANCHOR_END: use_storage