Expand description
§Dioxus Hooks
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:
Location | Where can you access the state? | Recommended for Libraries? | Examples |
---|---|---|---|
Hooks | Any components you pass it to | ✅ | use_signal(|| 0) , use_memo(|| state() * 2) |
Context | Any child components | ✅ | use_context_provider(|| Signal::new(0)) , use_context::<Signal<i32>>() |
Global | Anything in your app | ❌ | Signal::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:
Hook | Reactive (reruns when dependencies change) | Async | Memorizes Output | Example |
---|---|---|---|---|
use_memo | ✅ | ❌ | ✅ | use_memo(move || count() * 2) |
use_resource | ✅ | ✅ | ❌ | use_resource(move || reqwest::get(format!("/users/{user_id}"))) |
use_future | ❌ | ✅ | ❌ | use_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.
State | Sharable | Example |
---|---|---|
use_persistent | ❌ | use_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. - Unbounded
Receiver - The receiving end of an unbounded mpsc channel.
- Unbounded
Sender - The transmission end of an unbounded mpsc channel.
- UseFuture
Enums§
- UseFuture
State - A signal that represents the state of a future
- UseResource
State - 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.
- Dependency
Element - 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_ unmount Deprecated - 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.