Replies: 3 comments 3 replies
-
Why do you need to dispose of the root of the application? |
Beta Was this translation helpful? Give feedback.
-
I want to put some wasm things on my website just to showcase some things and experiment with wasm. But I use vueJS with my website and just wanted to show some leptos apps in some routes. I'm using SPA and wanted to avoid reloading the page if possible. Here is one example I have using leptos: https://glmachado.com/wasm-chart |
Beta Was this translation helpful? Give feedback.
-
My use-case for this is to have a component inside a React application that is written in Rust. For #[cfg(target_arch = "wasm32")]
pub fn mount_to<F, N>(parent: web_sys::HtmlElement, f: F) -> leptos::Disposer
where
F: Fn() -> N + 'static,
N: leptos::IntoView,
{
leptos::create_effect(move |last_node|
// only render once in case effect is run again
last_node.unwrap_or_else(|| {
let node = f().into_view();
parent.append_child(&leptos_dom::Mountable::get_mountable_node(&node)).unwrap();
// store node in effect, so disposing the effect will drop the node
node
}))
// convert effect into Disposer to dispose it on drop
.into()
} with the dependencies: leptos = { version="0.5.0-beta2", features = ["csr"] }
leptos_dom = { version="0.5.0-beta2", features = ["csr"] }
web-sys = { version = "0.3.64", features = ["HtmlElement"] }
Then I call this with: let disposer = mount_to(parent, || view! { <App/> }); Whenever Then I can move the use wasm_bindgen::prelude::*;
#[wasm_bindgen(js_name = "App")]
pub struct JsApp {
disposer: Option<leptos::Disposer>,
}
impl From<leptos::Disposer> for JsApp {
fn from(disposer: leptos::Disposer) -> Self {
Self {
disposer: Some(disposer),
}
}
}
#[wasm_bindgen(js_class = "App")]
impl JsApp {
/// For rendering a view that will never be unmounted from the dom use this.
pub fn leak(&mut self) {
std::mem::forget(self.disposer.take());
}
} Using export class App {
free(): void;
/**
* For rendering a view that will never be unmounted from the dom use this.
*/
leak(): void;
} Now I can use Edit: fixed implementation of |
Beta Was this translation helpful? Give feedback.
-
When we mount an application with
mont_to
ormount_to_body
, the context disposer is forgeted by themount_to
function. Why not returning this from the function? How am I supose to dispose an application without this?Beta Was this translation helpful? Give feedback.
All reactions