updated
This commit is contained in:
1 file changed
+9
-5
@@ -1,14 +1,12 @@
|
|||||||
# UseEffect
|
# 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.
|
[`use_effect`](https://docs.rs/dioxus-hooks/latest/dioxus_hooks/fn.use_effect.html) let's you run a callback that returns a future, only it's [dependencies](#dependencies) change. 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 callback 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 it when any of those dependencies change.
|
||||||
|
|
||||||
Example:
|
## Example
|
||||||
|
|
||||||
```rust, no_run
|
```rust, no_run
|
||||||
#[inline_props]
|
#[inline_props]
|
||||||
@@ -24,6 +22,12 @@ fn Profile(cx: Scope, id: usize) -> Element {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Because the dependencies are empty, this will only run once.
|
||||||
|
// An empty tuple is always equal to an empty tuple.
|
||||||
|
use_effect(cx, (), |()| async move {
|
||||||
|
println!("Hello, World!");
|
||||||
|
});
|
||||||
|
|
||||||
let name = name.get().clone().unwrap_or("Loading...".to_string());
|
let name = name.get().clone().unwrap_or("Loading...".to_string());
|
||||||
|
|
||||||
render!(
|
render!(
|
||||||
|
|||||||
Reference in new issue
Block a user