Struct leptos_dom::NodeRef

source ·
pub struct NodeRef(_);
Expand description

Contains a shared reference to a DOM node creating while using the view macro to create your UI.

#[component]
pub fn MyComponent(cx: Scope) -> Element {
  let input_ref = NodeRef::new(cx);

  let on_click = move |_| {
    let node = input_ref
      .get()
      .expect("input_ref should be loaded by now")
      .unchecked_into::<web_sys::HtmlInputElement>();
    log!("value is {:?}", node.value())
  };

  view! {
    cx,
    <div>
    // `node_ref` loads the input
    <input _ref=input_ref type="text"/>
    // the button consumes it
    <button on:click=on_click>"Click me"</button>
    </div>
  }
}

Implementations§

Creates an empty reference.

Gets the element that is currently stored in the reference.

This tracks reactively, so that node references can be used in effects. Initially, the value will be None, but once it is loaded the effect will rerun and its value will be Some(Element).

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
🔬This is a nightly-only experimental API. (fn_traits)
Performs the call operation.
🔬This is a nightly-only experimental API. (fn_traits)
Performs the call operation.
The returned type after the call operator is used.
🔬This is a nightly-only experimental API. (fn_traits)
Performs the call operation.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.