Files
dioxus/packages/hooks/src/use_set_compare.rs
T
Evan Almloff 0127501dbf Improve inline docs (#2460)
Improve inline docs

* improve incorrect event handler return error message

* Improve event handler docs

* document the eval functions

* document spawn and common spawn errors

* fix event handler docs

* add notes about how you use attributes and elements in rsx

* add doc aliases for attributes and events we rename

* add some more aliases for common search terms

* don't doc ignore any public examples in core

* don't ignore public doc examples in ssr

* don't ignore examples in the dioxus package readme

* add a warning when you launch without a renderer enabled

* fix some outdated element docs

* add a bunch of examples to resource

* add notes about desktop events

* add more docs for use_resource

* add on_unimplemented hint to Dependency

* fix some unresolved links

* add examples to each of the router traits

* add not implemented errors for router traits

* add an example to the routable trait

* expand rsx macro docs

* improve memo docs

* update the dioxus readme

* mention dioxus crate features in the docs

* fix a bunch of doc tests

* fix html doc tests

* fix router doc tests

* fix dioxus signals doc tests

* fix dioxus ssr doc tests

* fix use_future example in the hooks cheat sheet

* add a javascript alias for eval

* fix hook explanation values

* remove unused embed-doc-image dependency
2024-06-06 18:15:17 -07:00

58 lines
2.0 KiB
Rust

use std::hash::Hash;
use dioxus_core::prelude::*;
use dioxus_signals::{ReadOnlySignal, SetCompare};
/// Creates a new SetCompare which efficiently tracks when a value changes to check if it is equal to a set of values.
///
/// 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.
///
/// ```rust
/// use dioxus::prelude::*;
///
/// fn App() -> Element {
/// let mut count = use_signal(|| 0);
/// let compare = use_set_compare(move || count());
///
/// rsx! {
/// for i in 0..10 {
/// // Child will only re-render when i == count
/// Child { compare, i }
/// }
/// button {
/// // This will only rerender the child with the old and new value of i == count
/// // Because we are using a set compare, this will be O(1) instead of the O(n) performance of a selector
/// onclick: move |_| count += 1,
/// "Increment"
/// }
/// }
/// }
///
/// #[component]
/// fn Child(i: usize, compare: SetCompare<usize>) -> Element {
/// let active = use_set_compare_equal(i, compare);
/// if active() {
/// rsx! { "Active" }
/// } else {
/// rsx! { "Inactive" }
/// }
/// }
/// ```
#[doc = include_str!("../docs/rules_of_hooks.md")]
#[doc = include_str!("../docs/moving_state_around.md")]
#[must_use]
pub fn use_set_compare<R: Eq + Hash>(f: impl FnMut() -> R + 'static) -> SetCompare<R> {
use_hook(move || SetCompare::new(f))
}
/// A hook that returns true if the value is equal to the value in the set compare.
#[doc = include_str!("../docs/rules_of_hooks.md")]
#[doc = include_str!("../docs/moving_state_around.md")]
#[must_use]
pub fn use_set_compare_equal<R: Eq + Hash>(
value: R,
mut compare: SetCompare<R>,
) -> ReadOnlySignal<bool> {
use_hook(move || compare.equal(value))
}