Add use_on_mount and fix some typos (#1497)
* Add `use_on_mount` and fix some typos * Optimize away UseOnMount struct * `use_on_mount`, `use_on_unmount` -> `use_on_create`, `use_on_destroy` * Make `use_on_unmount` deprecated --------- Co-authored-by: Evan Almloff <evanalmloff@gmail.com>
This commit is contained in:
1 parent
9f7a0a9f63
commit
30a3283438
4 files changed
+58
-13
No files matched your search
@@ -0,0 +1,27 @@
|
||||
use dioxus_core::ScopeState;
|
||||
use std::cell::Cell;
|
||||
use std::future::Future;
|
||||
|
||||
/// A hook that runs a future when the component is mounted.
|
||||
///
|
||||
/// This is just [`use_effect`](crate::use_effect), but with no dependencies.
|
||||
/// If you have no dependencies, it's recommended to use this, not just because it's more readable,
|
||||
/// but also because it's a tiny bit more efficient.
|
||||
pub fn use_on_create<T, F>(cx: &ScopeState, future: impl FnOnce() -> F)
|
||||
where
|
||||
T: 'static,
|
||||
F: Future<Output = T> + 'static,
|
||||
{
|
||||
let needs_regen = cx.use_hook(|| Cell::new(true));
|
||||
|
||||
if needs_regen.get() {
|
||||
// We don't need regen anymore
|
||||
needs_regen.set(false);
|
||||
|
||||
let fut = future();
|
||||
|
||||
cx.push_future(async move {
|
||||
fut.await;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user