pub struct NodeRef<T: ?Sized> { /* private fields */ }Expand description
A reactive handle to a mounted DOM element.
NodeRef is created via App::use_node_ref (which routes through the
current HookContext) and is populated by the renderer after the
corresponding virtual node is mounted into the real DOM. Before
the first mount the inner value is None; after unmount it is reset
to None again, so consumers can rely on get() returning None
to detect the unmounted state.
The type parameter T is purely a phantom marker that names the
expected element type (e.g. NodeRef<HtmlInputElement>). The runtime
stores the element as a raw JsValue; calling get_cloned performs
the dyn_into cast on demand. This avoids pulling in web_sys types
in the core hot path and keeps the type zero-cost when the consumer
only needs the raw JsValue.
NodeRef is Clone and cheap to copy (it is an Rc clone). All clones
share the same underlying cell, so setting the value through one clone
is visible through every other clone.
Implementations§
Source§impl<T: ?Sized> NodeRef<T>
impl<T: ?Sized> NodeRef<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty NodeRef.
This constructor is pub so that Default::default() and
App::use_node_ref() can both produce handles. Callers should not
normally need to invoke this directly — use App::use_node_ref
inside a component so the handle participates in the hook order.
Sourcepub fn get(&self) -> Option<JsValue>
pub fn get(&self) -> Option<JsValue>
Returns a clone of the raw JsValue if an element is currently
attached, otherwise None.
Use this when you only need the underlying DOM element without
caring about its concrete type (e.g., passing it to a third-party
JS interop function). For type-safe access, use get_cloned.
§Returns
Option<JsValue>- The current value (or a snapshot thereof).
Sourcepub fn get_cloned(&self) -> Option<T>where
T: JsCast,
pub fn get_cloned(&self) -> Option<T>where
T: JsCast,
Returns a clone of the attached element cast to T, or None if
no element is attached or the cast fails.
The cast uses JsCast::dyn_into and discards the Err arm — a
failed cast is reported as None rather than panicking, which
matches React/Yew behaviour and avoids crashing the renderer on
ref misuse.
§Returns
Option<T>- A cloned copy of the inner value, if present.
Sourcepub fn set(&self, value: JsValue)
pub fn set(&self, value: JsValue)
Stores the given element as the current value of the handle.
This is called by the renderer after a ref: attribute fires;
users should not normally need to call it directly. Setting the
value clears any previous element first — multiple mounts of the
same NodeRef therefore always reflect the most recent element.
§Arguments
JsValue- AJsValueparameter.
Trait Implementations§
Source§impl<T: ?Sized> Clone for NodeRef<T>
Manual Clone impl: T is ?Sized so the derive macro (which
requires T: Clone) cannot be used. Rc clone is cheap and shares
the underlying cell with all clones.
impl<T: ?Sized> Clone for NodeRef<T>
Manual Clone impl: T is ?Sized so the derive macro (which
requires T: Clone) cannot be used. Rc clone is cheap and shares
the underlying cell with all clones.
Source§impl<T: ?Sized> Default for NodeRef<T>
impl<T: ?Sized> Default for NodeRef<T>
Source§fn default() -> Self
fn default() -> Self
Returns an empty NodeRef (no element associated).
The returned handle is independent of any hook context: it is not
registered as a hook and will never be populated by the renderer
unless it is the same instance that was returned by
App::use_node_ref and then later attached via a ref: attribute.
Prefer App::use_node_ref inside a component for normal usage.
Source§impl<T: ?Sized> From<NodeRef<T>> for AttributeValue
Converts a NodeRef<T> into an AttributeValue::Ref.
impl<T: ?Sized> From<NodeRef<T>> for AttributeValue
Converts a NodeRef<T> into an AttributeValue::Ref.
The type parameter T is erased at the AttributeValue layer (we
store the underlying JsValue cell), so any concrete element type
works. The html! macro relies on this to accept
html! { input { ref: my_ref } } without the user needing to call
.into() explicitly.