diff --git a/packages/desktop/src/desktop_context.rs b/packages/desktop/src/desktop_context.rs index 4475fe978..4ad2ff3a7 100644 --- a/packages/desktop/src/desktop_context.rs +++ b/packages/desktop/src/desktop_context.rs @@ -200,8 +200,8 @@ pub(super) fn handler( } /// Get a closure that executes any JavaScript in the WebView context. -pub fn use_eval(cx: &ScopeState) -> impl Fn(S) + '_ { - let desktop = use_window(&cx); +pub fn use_eval(cx: &ScopeState) -> &dyn Fn(S) { + let desktop = use_window(&cx).clone(); - move |script| desktop.eval(script) + cx.use_hook(|_| move |script| desktop.eval(script)) } diff --git a/packages/web/src/util.rs b/packages/web/src/util.rs index 3e8f92806..3e7232035 100644 --- a/packages/web/src/util.rs +++ b/packages/web/src/util.rs @@ -15,10 +15,12 @@ use dioxus_core::*; /// /// The closure will panic if the provided script is not valid JavaScript code /// or if it returns an uncaught error. -pub fn use_eval(_cx: &ScopeState) -> impl Fn(S) { - |script| { - js_sys::Function::new_no_args(&script.to_string()) - .call0(&wasm_bindgen::JsValue::NULL) - .expect("failed to eval script"); - } +pub fn use_eval(cx: &ScopeState) -> &dyn Fn(S) { + cx.use_hook(|_| { + |script: S| { + js_sys::Function::new_no_args(&script.to_string()) + .call0(&wasm_bindgen::JsValue::NULL) + .expect("failed to eval script"); + } + }) }