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_reactivethat merges uses the closure syntax to elaborate the dependency array
Structs§
- Action
- Coroutine
- Dispatching
- Effect
- A handle to an effect.
- Resource
- A handle to a reactive future spawned with use_resourcethat 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
- UseWaker
Enums§
- UseFutureState 
- A signal that represents the state of a future
- UseResourceState 
- A signal that represents the state of the resource
Traits§
- ActionCallback 
- 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_action 
- use_after_ suspense_ resolved 
- Run a closure after the suspense boundary this is under is resolved. The closure will be run immediately if the suspense boundary is already resolved or the scope is not under a suspense boundary.
- 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_coroutinefor 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 utility lifecycle hook that is intended to be used inside other hooks to determine if the outer hook has ran this render. The provided callback is executed after each render. The value will only be true if the containing outer hook is executed.
- 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.
- use_waker 
- A hook that provides a waker for other hooks to provide async/await capabilities.