pub struct NodeRef<G>(_)
where
    G: GenericNode
;
Expand description

A reference to a GenericNode. This allows imperatively accessing the node.

Example

#[component]
fn Component<G: Html>(cx: Scope) -> View<G> {
    let div_ref = create_node_ref(cx);
    view! { cx,
        div(ref=div_ref)
    }
}

Implementations

Creates an empty node ref.

Generally, it is preferable to use create_node_ref instead. Unlike create_node_ref, this creates a node ref that is not behind a reference. This makes it harder to pass around but can be desireable in certain cases.

Gets the raw node stored inside the node ref.

This attempts to cast the node to the specified type.

Example

Node refs are generally meant to be accessed in callbacks or in on_mount. Accessing the node ref directly in the body of the component will panic because the node ref has not yet been set.

let div_ref = create_node_ref(cx);
on_mount(cx, || {
    let node = div_ref.get::<DomNode>();
});
view! { cx,
    div(ref=div_ref)
}
Panics

Panics if the node ref is not set yet or is the wrong type.

For a non panicking version, see NodeRef::try_get.

Tries to get the T stored inside the node ref or None if it is not yet set or the wrong type.

For a panicking version, see NodeRef::get.

Gets the raw node stored inside the node ref.

Panics

Panics if the node ref is not set yet.

For a non panicking version, see NodeRef::try_get_raw.

Tries to get the raw node stored inside the node ref or None if it is not yet set.

For a panicking version, see NodeRef::get.

Sets the node ref with the specified node.

This method should be rarely used. Instead, use the ref= syntax in the view! macro to set the node.

Example

Setting the node using the ref= syntax:

#[component]
fn Component<G: Html>(cx: Scope) -> View<G> {
    let div_ref = create_node_ref(cx);
    view! { cx,
        div(ref=div_ref) // This assigns the node ref a value.
    }
}

Calling .set(...) imperatively:

#[component]
fn Component<G: Html>(cx: Scope) -> View<G> {
    let div = G::element::<html::div>();
    let div_ref = create_node_ref(cx);
    div_ref.set(div.clone());
    View::new_node(div)
}

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

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
Compare self to key and return true if they are equal.

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.