dioxus/packages/hooks
2021-12-15 16:07:09 -05:00
..
src docs: update local examples and docs to support new syntaxes 2021-12-15 15:56:53 -05:00
Cargo.toml fix: keyword length 2021-12-15 16:06:13 -05:00
CHANGELOG.md Release dioxus-core v0.1.3, dioxus-core-macro v0.1.2, dioxus-html v0.1.0, dioxus-desktop v0.0.0, dioxus-hooks v0.1.3, dioxus-liveview v0.1.0, dioxus-mobile v0.0.0, dioxus-router v0.1.0, dioxus-ssr v0.1.0, dioxus-web v0.0.0, dioxus v0.1.1 2021-12-15 16:07:09 -05:00
README.md docs: update local examples and docs to support new syntaxes 2021-12-15 15:56:53 -05:00

Common hooks for Dioxus

This crate includes some basic useful hooks for dioxus:

  • use_state
  • use_ref
  • use_collection
  • use_task
  • use_signal

use_state

The king daddy of state hooks.

You can always use it "normally" with the split method:

// Normal usage:
let value = use_state(&cx, || 10);

// "Classic" usage:
let (value, set_value) = use_state(&cx, || 0).split();

use_ref

use_rwlock

A multithreaded form of RwLock for use in tasks

let val = use_rwlock(cx, || 10);
use_task((), || async loop {
    *val.write().unwrap() += 1;
    async_std::task::delay(Duration::from_ms(1000)).await;
});
use_task((), || async loop {
    *val.write().unwrap() -= 1;
    async_std::task::delay(Duration::from_ms(500)).await;
});

use_hashmap

Store a memoized collection with similar semantics to use_state. Comes with a bunch of utility methods to make working with collections easier. Is essentially a wrapper over the immutable hashmap in im-rc.

let todos = use_hashmap(cx, |map| map.insert("bob", "bill"));
cx.render(rsx!(
    button { onclick: move |_| todos.insert("bob", "bill")
        "add random todo"
    }
)

use_task

use_task submits a task to the dioxus task queue to be progressed during Dioxus's async event loop. The task must not return anything

use_signal