I'm new to Yew and Rust, I wanted to write a component that would emit a canvas and pre-initialise it:
use yew;
use web_sys;
#[yew::function_component(App)]
fn app() -> yew::Html {
yew::html!{ <canvas id="main" onload={yew::Callback::from(|_| {
web_sys::console::log_1(&"Hello, World".into());
})}></canvas> }
}
fn main() {
yew::start_app::<App>();
}
However the event is never actually emitted and it's not even defined as the onload
property of the canvas in the actual rendered DOM, and I don't understand why.
I tried to rewrite the way the callback is registered by tying it to the window instead of the component:
use yew;
use web_sys;
use wasm_bindgen::{closure::Closure, UnwrapThrowExt};
#[yew::function_component(App)]
fn app() -> yew::Html {
web_sys::window()
.expect_throw("global window must be set")
.set_onload(Some(&Closure::<dyn Fn(web_sys::Event)>::new(|_| {
web_sys::console::log_1(&"Hello, World".into());
}).into_js_value().into()));
yew::html!{ <canvas id="main"></canvas> }
}
fn main() {
yew::start_app::<App>();
}
Now the callback is present in the window.onload
property, but it's still never called. Any help with this would be appreciated.