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.
## 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.
@ -13,23 +12,26 @@ Example:
```rust, no_run
#[inline_props]
fn Profile(cx: Scope, id: &str) -> Element {
let name = use_state(cx, || "Default name");
fn Profile(cx: Scope, id: usize) -> Element {
let name = use_state(cx, || None);
// Only fetch the user data when the id changes.
use_effect(cx, (id,), |(id,)| async move {
let user = fetch_user(id).await;
name.set(user.name);
use_effect(cx, (id,), |(id,)| {
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!(
p { "{name}" }
)
}
fn app(cx: Scope) -> Element {
render!(
Profile { id: "dioxusLabs" }
)
render!(Profile { id: 0 })
}
```
```

View file

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

View file

@ -14,25 +14,28 @@ use crate::UseFutureDep;
/// ## Examples
///
/// ```rust, no_run
///
/// #[inline_props]
/// fn Profile(cx: Scope, id: &str) -> Element {
/// let name = use_state(cx, || "Default name");
/// fn Profile(cx: Scope, id: usize) -> Element {
/// let name = use_state(cx, || None);
///
/// use_effect(cx, (id,), |(id,)| async move {
/// let user = fetch_user(id).await;
/// name.set(user.name);
/// // Only fetch the user data when the id changes.
/// use_effect(cx, (id,), |(id,)| {
/// 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!(
/// p { "{name}" }
/// )
/// }
///
/// fn app(cx: Scope) -> Element {
/// render!(
/// Profile { id: "dioxusLabs" }
/// )
/// render!(Profile { id: 0 })
/// }
/// ```
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,)| {
/// // This will only be calculated when `number` has changed.
/// number * 100
/// }):
///
/// });
/// render!(
/// p { "{bigger_number}" }
/// )
/// }
///
/// fn app(cx: Scope) -> Element {
/// render!(
/// Calculator { number: 0 }
/// )
/// render!(Calculator { number: 0 })
/// }
/// ```
pub fn use_memo<T, D>(cx: &ScopeState, dependencies: D, callback: impl FnOnce(D::Out) -> T) -> &T