Crate dioxus_hooks

Source
Expand description

§Dioxus Hooks

Crates.io MIT licensed Build Status Discord chat

Website | Guides | API Docs | Chat

§Overview

dioxus-hooks includes some basic useful hooks for Dioxus such as:

  • use_signal
  • use_effect
  • use_resource
  • use_memo
  • use_coroutine

Unlike React, none of these hooks are foundational since they all build off the primitive use_hook. You can extend these hooks with custom hooks in your own code. If you think they would be useful for the broader community, you can open a PR to add your hook to the Dioxus Awesome list.

§State Cheat Sheet

If you aren’t sure what hook to use, you can use this cheat sheet to help you decide:

§State Location

Depending on where you need to access the state, you can put your state in one of three places:

LocationWhere can you access the state?Recommended for Libraries?Examples
HooksAny components you pass it touse_signal(|| 0), use_memo(|| state() * 2)
ContextAny child componentsuse_context_provider(|| Signal::new(0)), use_context::<Signal<i32>>()
GlobalAnything in your appSignal::global(|| 0)

§Derived State

If you don’t have an initial value for your state, you can derive your state from other states with a closure or asynchronous function:

HookReactive (reruns when dependencies change)AsyncMemorizes OutputExample
use_memouse_memo(move || count() * 2)
use_resourceuse_resource(move || reqwest::get(format!("/users/{user_id}")))
use_futureuse_future(move || println!("{:?}", reqwest::get(format!("/users/{user_id}"))))

§Persistent State

The core hooks library doesn’t provide hooks for persistent state, but you can extend the core hooks with hooks from dioxus-sdk and the dioxus-router to provide persistent state management.

StateSharableExample
use_persistentuse_persistent("unique_key", move || initial_state)
Router<Route> {}#[derive(Routable, Clone, PartialEq)] enum Route { #[route("/user/:id")] Homepage { id: u32 } }

§Contributing

  • Report issues on our issue tracker.
  • Join the discord and ask questions!

§License

This project is licensed under the MIT license.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Dioxus by you shall be licensed as MIT without any additional terms or conditions.

Macros§

to_owned
A helper macro for cloning multiple values at once
use_reactive
A helper macro for use_reactive that merges uses the closure syntax to elaborate the dependency array

Structs§

Coroutine
Effect
A handle to an effect.
Resource
A handle to a reactive future spawned with use_resource that can be used to modify or read the result of the future.
UnboundedReceiver
The receiving end of an unbounded mpsc channel.
UnboundedSender
The transmission end of an unbounded mpsc channel.
UseFuture

Enums§

UseFutureState
A signal that represents the state of a future
UseResourceState
A signal that represents the state of the resource

Traits§

Dependency
A dependency is a trait that can be used to determine if a effect or selector should be re-run.
DependencyElement
A dependency is a trait that can be used to determine if a effect or selector should be re-run.

Functions§

try_use_context
Consume some context in the tree, providing a sharable handle to the value
use_callback
Create a callback that’s always up to date. Whenever this hook is called the inner callback will be replaced with the new callback but the handle will remain.
use_context
Consume some context in the tree, providing a sharable handle to the value
use_context_provider
Provide some context via the tree and return a reference to it
use_coroutine
Maintain a handle over a future that can be paused, resumed, and canceled.
use_coroutine_handle
Get a handle to a coroutine higher in the tree Analogous to use_context_provider and use_context, but used for coroutines specifically See the docs for use_coroutine for more details.
use_effect
Effects are reactive closures that run after the component has finished rendering. Effects are useful for things like manually updating the DOM after it is rendered with web-sys or javascript. Or reading a value from the rendered DOM.
use_future
A hook that allows you to spawn a future the first time you render a component.
use_hook_did_run
A hook that uses before/after lifecycle hooks to determine if the hook was run
use_memo
Creates a new Memo. The memo will be run immediately and whenever any signal it reads is written to. Memos can be used to efficiently compute derived data from signals.
use_on_unmountDeprecated
use_reactive
Takes some non-reactive data, and a closure and returns a closure that will subscribe to that non-reactive data as if it were reactive.
use_resource
use_resource() is a reactive hook that resolves to the result of a future. It will rerun when you write to any signals you read inside the future.
use_root_context
Try to get a value from the root of the virtual dom, if it doesn’t exist, create a new one with the closure provided.
use_set_compare
Creates a new SetCompare which efficiently tracks when a value changes to check if it is equal to a set of values.
use_set_compare_equal
A hook that returns true if the value is equal to the value in the set compare.
use_signal
Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
use_signal_sync
Creates a new `Send + Sync`` Signal. Signals are a Copy state management solution with automatic dependency tracking.