dioxus_hooks/
use_set_compare.rs

1use std::hash::Hash;
2
3use dioxus_core::prelude::*;
4use dioxus_signals::{ReadOnlySignal, SetCompare};
5
6/// Creates a new SetCompare which efficiently tracks when a value changes to check if it is equal to a set of values.
7///
8/// Generally, you shouldn't need to use this hook. Instead you can use [`crate::use_memo()`]. If you have many values that you need to compare to a single value, this hook will change updates from O(n) to O(1) where n is the number of values you are comparing to.
9///
10/// ```rust
11/// use dioxus::prelude::*;
12///
13/// fn App() -> Element {
14///     let mut count = use_signal(|| 0);
15///     let compare = use_set_compare(move || count());
16///
17///     rsx! {
18///         for i in 0..10 {
19///             // Child will only re-render when i == count
20///             Child { compare, i }
21///         }
22///         button {
23///             // This will only rerender the child with the old and new value of i == count
24///             // Because we are using a set compare, this will be O(1) instead of the O(n) performance of a selector
25///             onclick: move |_| count += 1,
26///             "Increment"
27///         }
28///     }
29/// }
30///
31/// #[component]
32/// fn Child(i: usize, compare: SetCompare<usize>) -> Element {
33///     let active = use_set_compare_equal(i, compare);
34///     if active() {
35///         rsx! { "Active" }
36///     } else {
37///         rsx! { "Inactive" }
38///     }
39/// }
40/// ```
41#[doc = include_str!("../docs/rules_of_hooks.md")]
42#[doc = include_str!("../docs/moving_state_around.md")]
43#[must_use]
44pub fn use_set_compare<R: Eq + Hash>(f: impl FnMut() -> R + 'static) -> SetCompare<R> {
45    use_hook(move || SetCompare::new(f))
46}
47
48/// A hook that returns true if the value is equal to the value in the set compare.
49#[doc = include_str!("../docs/rules_of_hooks.md")]
50#[doc = include_str!("../docs/moving_state_around.md")]
51#[must_use]
52pub fn use_set_compare_equal<R: Eq + Hash>(
53    value: R,
54    mut compare: SetCompare<R>,
55) -> ReadOnlySignal<bool> {
56    use_hook(move || compare.equal(value))
57}