dioxus/packages/hooks
Marc Espin 91112734f6
Revert "feat: Manual impl of PartialEq for Coroutine" (#2899)
* Revert "feat: Manual impl of PartialEq for `Coroutine` (#2895)"

This reverts commit e6efd973d8.

* Update use_coroutine.rs

* Update use_coroutine.rs

* Update use_coroutine.rs
2024-08-28 16:09:31 +00:00
..
docs Improve inline docs (#2460) 2024-06-06 18:15:17 -07:00
src Revert "feat: Manual impl of PartialEq for Coroutine" (#2899) 2024-08-28 16:09:31 +00:00
tests Deduplicate reactive scope updates/Reset subscriptions on reruns/fix use memo double update (#2506) 2024-06-18 18:49:25 -07:00
Cargo.toml Pre-release 0.6.0-alpha.0 (#2755) 2024-07-31 22:37:39 -05:00
README.md Fix Links & Add Link Checker (#2769) 2024-08-02 10:46:18 -07:00

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:

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.