This PR contains just the breaking changes required for the upcoming store implementation. It adds two new primitives: Mutable mapped signals (without a different subscriber list). This lets you scope a writable value and pass it to a child component. It requires two closures to map the immutable and mutable references to the inner signal. With stores, these closures will be derived Boxed readable and writable values (ReadSignal, and WriteSignal). Any readable or writable types can be converted to a read signal or write signal with the boxed method or built in types are automatically converted into the boxed variants when passing into props * map signals mutably * boxed signals * remove mutable borrow type from boxed writable * improve map mut example * restore boxed_mut helper * make boxed readable/writable types copy * Move away from the ReadOnlySignal alias in examples * automatically convert Write * Fix some tests * ReadSignal and WriteSignal * move read only signal alias * make maps copy * store prototype * implement derive macro * Handle generics in the derive macro * Make Selector boxable * derive boxed conversion * Get rid of store type * Remove hardcoded type from derive macro * selector -> store * Refactor store implementation * more vec methods * todo mvc store example * Implement Storable for HashMap * fix write * restore sorting logic in todomvc * derive readable and writable * improve todomvc store example * Result store * Option store * collect dead subscribers * fix clippy and formatting * recognize some common foreign types automatically * fix foreign stores in props * slice and array selectors * clean up store example * remove stores * writable is child owned * fix typo * fix clippy * fix doc tests
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.