Skip to main content

NodeRef

Struct NodeRef 

Source
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>

Source

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.

Source

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).
Source

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.
Source

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 - A JsValue parameter.
Source

pub fn clear(&self)

Clears the currently attached element, if any.

Called by the renderer when a node is unmounted. After clear, get and [get_cloned] both return None until the next set call.

Source

pub fn is_set(&self) -> bool

Returns true if an element is currently attached to this handle.

§Returns
  • bool - true when the value has been initialised.

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.

Source§

fn clone(&self) -> Self

Clones the NodeRef by reusing shared, cheap-to-clone state where possible.

1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: ?Sized> Debug for NodeRef<T>

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the NodeRef via the supplied formatter.

§Arguments
  • &mut Formatter<'_> - The formatter receiving the formatted output.
§Returns
  • fmt::Result - Result of the formatting operation.
Source§

impl<T: ?Sized> Default for NodeRef<T>

Source§

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.

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.

Source§

fn from(node_ref: NodeRef<T>) -> Self

Wraps the ref cell as an AttributeValue::Ref so the renderer can populate it after mount.

§Returns
  • AttributeValue - A ref variant carrying the (still empty) handle.
§Arguments
  • NodeRef<T> - Input value to convert from.

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for NodeRef<T>

§

impl<T> !Send for NodeRef<T>

§

impl<T> !Sync for NodeRef<T>

§

impl<T> !UnwindSafe for NodeRef<T>

§

impl<T> Freeze for NodeRef<T>
where PhantomData<fn() -> T>: Freeze, T: ?Sized,

§

impl<T> Unpin for NodeRef<T>
where PhantomData<fn() -> T>: Unpin, T: ?Sized,

§

impl<T> UnsafeUnpin for NodeRef<T>
where PhantomData<fn() -> T>: UnsafeUnpin, T: ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more