Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
4 changed files
with
58 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters