feat(docs): Improved shared state, use_effect and use_memo docs

This commit is contained in:
marc2332 committed 2023-06-28 00:15:00 +02:00
1 parent dfa4d8d989
commit 8a2f9f3fcb
7 files changed
+158 -19

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
# UseEffect
[`use_effect`](https://docs.rs/dioxus-hooks/latest/dioxus_hooks/fn.use_effect.html) provides a future that executes after the hooks have been applied.
Whenever the hooks dependencies change, the future will be re-evaluated. This is useful to syncrhonize with external events.
If a future is pending when the dependencies change, the previous future will be allowed to continue
> The `dependencies` is tuple of references to values that are `PartialEq + Clone`.
```rust, no_run
#[inline_props]
fn Profile(cx: Scope, id: &str) -> Element {
let name = use_state(cx, || "Default name");
// 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);
});
render!(
p { "{name}" }
)
}
fn app(cx: Scope) -> Element {
render!(
Profile { id: "dioxusLabs" }
)
}
```