Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add use_on_mount and fix some typos #1497

Merged
merged 5 commits into from Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/hooks/src/lib.rs
@@ -1,7 +1,7 @@
#![cfg_attr(feature = "nightly-features", feature(debug_refcell))]

#[macro_export]
/// A helper macro for using hooks and properties in async environements.
/// A helper macro for using hooks and properties in async environments.
///
/// # Usage
///
Expand Down Expand Up @@ -86,3 +86,6 @@ pub use usememo::*;

mod userootcontext;
pub use userootcontext::*;

mod use_on_mount;
pub use use_on_mount::*;
34 changes: 34 additions & 0 deletions packages/hooks/src/use_on_mount.rs
@@ -0,0 +1,34 @@
use dioxus_core::{ScopeState, TaskId};
use std::cell::Cell;
use std::future::Future;

struct UseOnMount {
needs_regen: bool,
task: Cell<Option<TaskId>>,
}

/// 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_mount<T, F>(cx: &ScopeState, future: impl FnOnce() -> F)
where
T: 'static,
F: Future<Output = T> + 'static,
{
let state = cx.use_hook(move || UseOnMount {
needs_regen: true,
task: Cell::new(None),
});

if state.needs_regen {
// We don't need regen anymore
state.needs_regen = false;
let fut = future();

state.task.set(Some(cx.push_future(async move {
tigerros marked this conversation as resolved.
Show resolved Hide resolved
fut.await;
})));
}
}
11 changes: 8 additions & 3 deletions packages/hooks/src/useeffect.rs
Expand Up @@ -3,13 +3,18 @@ use std::{any::Any, cell::Cell, future::Future};

use crate::UseFutureDep;

/// A hook that provides a future that executes after the hooks have been applied
/// A hook that provides a future that executes after the hooks have been applied.
///
/// Whenever the hooks dependencies change, the future will be re-evaluated.
/// If a future is pending when the dependencies change, the previous future
/// will be allowed to continue
/// will be allowed to continue.
///
/// - dependencies: a tuple of references to values that are `PartialEq` + `Clone`
/// **Note:** If your dependency list is always empty, use [`use_on_mount`](crate::use_on_mount).
///
/// ## Arguments
///
/// - `dependencies`: a tuple of references to values that are `PartialEq` + `Clone`.
/// - `future`: a closure that takes the `dependencies` as arguments and returns a `'static` future.
///
/// ## Examples
///
Expand Down