Function 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({
      clones!(self.value, mut ref_container);

      move || {
        ref_container.current_mut().value = value;
      }
    }, Deps::some(self.value));

    let vnode = h!(div).build(
      ref_container.current().value
    );
    vnode
  }
}