1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use super::RefContainer;
use crate::{react_bindings, Context};
use std::{rc::Rc, thread::LocalKey};
use wasm_bindgen::UnwrapThrowExt;
pub fn use_context<T>(context: &'static LocalKey<Context<T>>) -> Rc<T> {
context.with(|context| {
let js_ref = react_bindings::use_context(context.as_ref());
let ref_container = unsafe {
RefContainer::<Rc<T>>::try_from_js_ref(&js_ref)
.expect_throw("trying to operate invalid ref container")
};
let value = ref_container.current();
value.clone()
})
}