Search code examples
wasm-bindgen

How to access web_sys::CustomEvent.detail data from rust


Have code which generates an https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.CustomEvent.html.

Logging event.detail() results in: {index: 1}. This is of type JsValue, but how to obtain the integer 1 value in rust?

One way could be https://rustwasm.github.io/docs/wasm-bindgen/reference/iterating-over-js-values.html Other betters ways?


Solution

  • js_sys::Reflect seems to fit for this: https://rustwasm.github.io/wasm-bindgen/api/js_sys/Reflect/fn.get.html

    let js_index = js_sys::Reflect::get(&event.detail(), &JsValue::from_str("index")).unwrap();    
    let i : usize = js_index.as_f64().unwrap() as usize;