cargo check all
This commit is contained in:
10 files changed
+123
-329
No files matched your search
@@ -132,3 +132,100 @@ pub fn schedule_update() -> Arc<dyn Fn() + Send + Sync> {
|
||||
pub fn schedule_update_any() -> Arc<dyn Fn(ScopeId) + Send + Sync> {
|
||||
Runtime::with_current_scope(|cx| cx.schedule_update_any()).expect("to be in a dioxus runtime")
|
||||
}
|
||||
|
||||
/// Creates a callback that will be run before the component is removed.
|
||||
/// This can be used to clean up side effects from the component
|
||||
/// (created with [`use_effect`](crate::use_effect)).
|
||||
///
|
||||
/// Example:
|
||||
/// ```rust
|
||||
/// use dioxus::prelude::*;
|
||||
///
|
||||
/// fn app() -> Element {
|
||||
/// let state = use_signal(|| true);
|
||||
/// rsx! {
|
||||
/// for _ in 0..100 {
|
||||
/// h1 {
|
||||
/// "spacer"
|
||||
/// }
|
||||
/// }
|
||||
/// if **state {
|
||||
/// rsx! {
|
||||
/// child_component {}
|
||||
/// }
|
||||
/// }
|
||||
/// button {
|
||||
/// onclick: move |_| {
|
||||
/// state.set(!*state.get());
|
||||
/// },
|
||||
/// "Unmount element"
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// fn child_component() -> Element {
|
||||
/// let original_scroll_position = use_signal(|| 0.0);
|
||||
/// use_effect((), move |_| {
|
||||
/// to_owned![original_scroll_position];
|
||||
/// async move {
|
||||
/// let window = web_sys::window().unwrap();
|
||||
/// let document = window.document().unwrap();
|
||||
/// let element = document.get_element_by_id("my_element").unwrap();
|
||||
/// element.scroll_into_view();
|
||||
/// original_scroll_position.set(window.scroll_y().unwrap());
|
||||
/// }
|
||||
/// });
|
||||
///
|
||||
/// use_on_destroy({
|
||||
/// to_owned![original_scroll_position];
|
||||
/// /// restore scroll to the top of the page
|
||||
/// move || {
|
||||
/// let window = web_sys::window().unwrap();
|
||||
/// window.scroll_with_x_and_y(*original_scroll_position.current(), 0.0);
|
||||
/// }
|
||||
/// });
|
||||
///
|
||||
/// rsx!{
|
||||
/// div {
|
||||
/// id: "my_element",
|
||||
/// "hello"
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub fn use_drop<D: FnOnce() + 'static>(destroy: D) {
|
||||
struct LifeCycle<D: FnOnce()> {
|
||||
/// Wrap the closure in an option so that we can take it out on drop.
|
||||
ondestroy: Option<D>,
|
||||
}
|
||||
|
||||
/// On drop, we want to run the closure.
|
||||
impl<D: FnOnce()> Drop for LifeCycle<D> {
|
||||
fn drop(&mut self) {
|
||||
if let Some(f) = self.ondestroy.take() {
|
||||
f();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// We need to impl clone for the lifecycle, but we don't want the drop handler for the closure to be called twice.
|
||||
impl<D: FnOnce()> Clone for LifeCycle<D> {
|
||||
fn clone(&self) -> Self {
|
||||
Self { ondestroy: None }
|
||||
}
|
||||
}
|
||||
|
||||
use_hook(|| LifeCycle {
|
||||
ondestroy: Some(destroy),
|
||||
});
|
||||
}
|
||||
|
||||
pub fn use_hook_with_cleanup<T: Clone + 'static>(
|
||||
hook: impl FnOnce() -> T,
|
||||
cleanup: impl FnOnce(T) + 'static,
|
||||
) -> T {
|
||||
let value = use_hook(hook);
|
||||
let _value = value.clone();
|
||||
use_drop(move || cleanup(_value));
|
||||
value
|
||||
}
|
||||
@@ -89,9 +89,10 @@ pub mod prelude {
|
||||
consume_context, consume_context_from_scope, current_scope_id, fc_to_builder, generation,
|
||||
has_context, needs_update, parent_scope, provide_context, provide_root_context,
|
||||
remove_future, schedule_update, schedule_update_any, spawn, spawn_forever, suspend,
|
||||
try_consume_context, use_error_boundary, use_hook, AnyValue, Attribute, Component,
|
||||
ComponentFunction, Element, ErrorBoundary, Event, EventHandler, Fragment, HasAttributes,
|
||||
IntoAttributeValue, IntoDynNode, Properties, Runtime, RuntimeGuard, ScopeId, ScopeState,
|
||||
Task, Template, TemplateAttribute, TemplateNode, Throw, VNode, VNodeInner, VirtualDom,
|
||||
try_consume_context, use_drop, use_error_boundary, use_hook, use_hook_with_cleanup,
|
||||
AnyValue, Attribute, Component, ComponentFunction, Element, ErrorBoundary, Event,
|
||||
EventHandler, Fragment, HasAttributes, IntoAttributeValue, IntoDynNode, Properties,
|
||||
Runtime, RuntimeGuard, ScopeId, ScopeState, Task, Template, TemplateAttribute,
|
||||
TemplateNode, Throw, VNode, VNodeInner, VirtualDom,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user