Function wasm_react::hooks::use_ref
source · [−]pub fn use_ref<T: 'static>(init: T) -> RefContainer<T>
Expand description
This is the main hook for persisting Rust data through the entire lifetime of the component.
Whenever the component is unmounted by React, the data will also be dropped.
Keep in mind that the inner value of use_ref()
can only be accessed in
Rust. If you need a ref to hold a DOM element (or a JS value in general),
use use_js_ref()
instead.
The component will not rerender when you mutate the underlying data. If you
want that, use use_state()
instead.
Example
impl Component for MyComponent {
fn render(&self) -> VNode {
let ref_container = use_ref(MyData {
value: "Hello World!"
});
use_effect({
let value = self.value;
let mut ref_container = ref_container.clone();
move || {
ref_container.current_mut().value = value;
|| ()
}
}, Deps::some(self.value));
h!(div).build(c![
ref_container.current().value
])
}
}