| Crates.io | more-yew-hooks |
| lib.rs | more-yew-hooks |
| version | 0.2.0 |
| created_at | 2025-10-02 19:07:40.757015+00 |
| updated_at | 2025-10-04 22:56:41.561793+00 |
| description | Additional hooks for the [`yew`](https://yew.rs) ecosystem, including bugfixes for [`yew-hooks`][yew-hooks]. |
| homepage | https://github.com/dra11y/more-yew-hooks |
| repository | https://github.com/dra11y/more-yew-hooks |
| max_upload_size | |
| id | 1864871 |
| size | 55,714 |
Additional hooks for the yew ecosystem, including bugfixes for yew-hooks.
I created this crate to augment some of the hooks in the yew_hooks, such as use_online and use_btree_set.
use_local_storage_default: returns T::default() on absence or deserialization failure and listens to storage events.use_session_storage_with_listen: listens for storage events and filters by sessionStorage in case keys conflict with localStorage.use_btree_set: ordered set state with operations (insert, replace, retain, etc.).use_online: minimal wrapper around navigator.onLine with event listeners.I was wrong about use_debounce and use_debounce_state in yew-hooks. They do accept FnOnce() and this is correct (least restrictive). De-confusion: FnOnce() can be called at least once, NOT at MOST once. The bug I experienced was likely somewhere else in my project. Thus they have been removed from this crate.
Add to your Cargo.toml:
[dependencies]
more-yew-hooks = "0.1"
Optional feature flags:
| Feature | Default | Purpose |
|---|---|---|
storage |
enabled | Enables hooks that serialize to Web Storage (serde, serde_json). |
If you disable default features and only want non-storage hooks:
more-yew-hooks = { version = "0.1", default-features = false }
Uses Rust edition 2024. Practically, you likely need Rust 1.81+ (exact MSRV still provisional until CI enforces). If you rely on an older toolchain, please file an issue.
use std::collections::BTreeSet;
use yew::prelude::*;
use more_yew_hooks::prelude::*; // (Consider adding a prelude re-export if desired)
#[function_component(App)]
fn app() -> Html {
let planets = more_yew_hooks::use_btree_set(BTreeSet::from([
"Mercury", "Venus", "Earth"
]));
let online = more_yew_hooks::use_online();
html! {
<div>
<p>{ format!("Online: {}", *online) }</p>
<p>{ format!("Planets: {:?}", planets.current().iter().collect::<Vec<_>>()) }</p>
</div>
}
}
use_btree_setTracks a BTreeSet<T> with convenient mutation helpers that automatically trigger rerenders. Order is preserved (in‑order traversal). Inspired by yew_hooks::use_set but using an ordered structure and exposing additional operations.
Signature:
fn use_btree_set<T: 'static + Eq + Ord + Hash>(initial: BTreeSet<T>) -> UseBTreeSetHandle<T>
Handle methods:
current() -> Ref<BTreeSet<T>> (borrow view)set(BTreeSet<T>)insert(T) -> boolreplace(T) -> Option<T> (returns replaced value)remove(&T) -> boolretain(F)clear()Example: See in-source docs for a full interactive example.
Edge cases:
current() while holding an outstanding mutable borrow (rare in normal hook usage).use_local_storage_default (feature = storage)Wrapper around localStorage that returns T::default() if the key is missing or deserialization fails. Listens to storage events.
Signature:
fn use_local_storage_default<T: DeserializeOwned + Default + 'static>(key: String) -> UseLocalStorageDefaultHandle<T>
Handle:
Tset(T) — serializes (JSON) & updates statedelete() — removes key and resets to T::default()Notes:
serde_json.log) storage updates for debugging.Edge cases:
T::default() instead of erroring.use_session_storage_with_listen (feature = storage)Session storage variant that listens for storage events and updates only when the event references the same storage area and key.
Signature:
fn use_session_storage_with_listen<T: DeserializeOwned + 'static>(key: String) -> UseSessionStorageWithListenerHandle<T>
Handle:
Option<T> (None when no value present)set(T)delete()Notes:
sessionStorage area (validated).use_onlineHook returning a UseStateHandle<bool> that reflects navigator.onLine and updates on online / offline events.
Signature:
fn use_online() -> UseStateHandle<bool>
Usage:
let online = use_online();
html! { <p>{ if *online { "Online" } else { "Offline" } }</p> };
Caveat: Browser navigator.onLine semantics vary (e.g., may report true behind captive portals). Treat as a hint, not a guarantee.
Contributions welcome! Suggested flow:
Run cargo fmt + cargo clippy --all-targets --all-features -- -D warnings before submitting.
Because hooks depend on wasm32-unknown-unknown + a browser environment:
rustup target add wasm32-unknown-unknown
# Example bundler commands go here (Trunk, Leptos CLI, etc.)
Consider writing integration tests in a harness crate with wasm-bindgen-test (future work for this repo).
MIT