updated docs

This commit is contained in:
marc2332 2023-07-02 23:00:26 +02:00
parent fb2669c3be
commit 052ae145bf
No known key found for this signature in database
GPG key ID: C06A66E2828F72E1
4 changed files with 28 additions and 31 deletions

View file

@ -4,7 +4,6 @@
Whenever the hooks [dependencies](#dependencies) change, the future will be re-evaluated. This is useful to syncrhonize with external events. Whenever the hooks [dependencies](#dependencies) change, the future will be re-evaluated. This is useful to syncrhonize with external events.
## Dependencies ## Dependencies
You can make the future re-run when some value changes. For example, you might want to fetch a user's data only when the user id changes. You can provide a tuple of "dependencies" to the hook. It will automatically re-run the future when any of those dependencies change. You can make the future re-run when some value changes. For example, you might want to fetch a user's data only when the user id changes. You can provide a tuple of "dependencies" to the hook. It will automatically re-run the future when any of those dependencies change.
@ -13,23 +12,26 @@ Example:
```rust, no_run ```rust, no_run
#[inline_props] #[inline_props]
fn Profile(cx: Scope, id: &str) -> Element { fn Profile(cx: Scope, id: usize) -> Element {
let name = use_state(cx, || "Default name"); let name = use_state(cx, || None);
// Only fetch the user data when the id changes. // Only fetch the user data when the id changes.
use_effect(cx, (id,), |(id,)| async move { use_effect(cx, (id,), |(id,)| {
let user = fetch_user(id).await; to_owned![name];
name.set(user.name); async move {
let user = fetch_user(id).await;
name.set(user.name);
}
}); });
let name = name.get().clone().unwrap_or("Loading...".to_string());
render!( render!(
p { "{name}" } p { "{name}" }
) )
} }
fn app(cx: Scope) -> Element { fn app(cx: Scope) -> Element {
render!( render!(Profile { id: 0 })
Profile { id: "dioxusLabs" }
)
} }
``` ```

View file

@ -8,16 +8,12 @@ fn Calculator(cx: Scope, number: usize) -> Element {
let bigger_number = use_memo(cx, (number,), |(number,)| { let bigger_number = use_memo(cx, (number,), |(number,)| {
// This will only be calculated when `number` has changed. // This will only be calculated when `number` has changed.
number * 100 number * 100
}): });
render!( render!(
p { "{bigger_number}" } p { "{bigger_number}" }
) )
} }
fn app(cx: Scope) -> Element { fn app(cx: Scope) -> Element {
render!( render!(Calculator { number: 0 })
Calculator { number: 0 }
)
} }
``` ```

View file

@ -14,25 +14,28 @@ use crate::UseFutureDep;
/// ## Examples /// ## Examples
/// ///
/// ```rust, no_run /// ```rust, no_run
///
/// #[inline_props] /// #[inline_props]
/// fn Profile(cx: Scope, id: &str) -> Element { /// fn Profile(cx: Scope, id: usize) -> Element {
/// let name = use_state(cx, || "Default name"); /// let name = use_state(cx, || None);
/// ///
/// use_effect(cx, (id,), |(id,)| async move { /// // Only fetch the user data when the id changes.
/// let user = fetch_user(id).await; /// use_effect(cx, (id,), |(id,)| {
/// name.set(user.name); /// to_owned![name];
/// async move {
/// let user = fetch_user(id).await;
/// name.set(user.name);
/// }
/// }); /// });
/// ///
/// let name = name.get().clone().unwrap_or("Loading...".to_string());
///
/// render!( /// render!(
/// p { "{name}" } /// p { "{name}" }
/// ) /// )
/// } /// }
/// ///
/// fn app(cx: Scope) -> Element { /// fn app(cx: Scope) -> Element {
/// render!( /// render!(Profile { id: 0 })
/// Profile { id: "dioxusLabs" }
/// )
/// } /// }
/// ``` /// ```
pub fn use_effect<T, F, D>(cx: &ScopeState, dependencies: D, future: impl FnOnce(D::Out) -> F) pub fn use_effect<T, F, D>(cx: &ScopeState, dependencies: D, future: impl FnOnce(D::Out) -> F)

View file

@ -16,17 +16,13 @@ use crate::UseFutureDep;
/// let bigger_number = use_memo(cx, (number,), |(number,)| { /// let bigger_number = use_memo(cx, (number,), |(number,)| {
/// // This will only be calculated when `number` has changed. /// // This will only be calculated when `number` has changed.
/// number * 100 /// number * 100
/// }): /// });
///
/// render!( /// render!(
/// p { "{bigger_number}" } /// p { "{bigger_number}" }
/// ) /// )
/// } /// }
///
/// fn app(cx: Scope) -> Element { /// fn app(cx: Scope) -> Element {
/// render!( /// render!(Calculator { number: 0 })
/// Calculator { number: 0 }
/// )
/// } /// }
/// ``` /// ```
pub fn use_memo<T, D>(cx: &ScopeState, dependencies: D, callback: impl FnOnce(D::Out) -> T) -> &T pub fn use_memo<T, D>(cx: &ScopeState, dependencies: D, callback: impl FnOnce(D::Out) -> T) -> &T