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
sourceimpl<G> NodeRef<G>where
G: GenericNode + Any,
impl<G> NodeRef<G>where
G: GenericNode + Any,
sourcepub fn new() -> NodeRef<G>
pub fn new() -> NodeRef<G>
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.
sourcepub fn get<T>(&self) -> Twhere
T: GenericNode,
pub fn get<T>(&self) -> Twhere
T: GenericNode,
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
.
sourcepub fn try_get<T>(&self) -> Option<T>where
T: GenericNode,
pub fn try_get<T>(&self) -> Option<T>where
T: GenericNode,
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
.
sourcepub fn get_raw(&self) -> G
pub fn get_raw(&self) -> G
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
.
sourcepub fn try_get_raw(&self) -> Option<G>
pub fn try_get_raw(&self) -> Option<G>
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
.
sourcepub fn set(&self, node: G)
pub fn set(&self, node: G)
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
sourceimpl<G> Clone for NodeRef<G>where
G: Clone + GenericNode,
impl<G> Clone for NodeRef<G>where
G: Clone + GenericNode,
sourceimpl<G> Debug for NodeRef<G>where
G: GenericNode,
impl<G> Debug for NodeRef<G>where
G: GenericNode,
sourceimpl<G> Default for NodeRef<G>where
G: GenericNode,
impl<G> Default for NodeRef<G>where
G: GenericNode,
sourceimpl<G> PartialEq<NodeRef<G>> for NodeRef<G>where
G: PartialEq<G> + GenericNode,
impl<G> PartialEq<NodeRef<G>> for NodeRef<G>where
G: PartialEq<G> + GenericNode,
impl<G> Eq for NodeRef<G>where
G: Eq + GenericNode,
impl<G> StructuralEq for NodeRef<G>where
G: GenericNode,
impl<G> StructuralPartialEq for NodeRef<G>where
G: GenericNode,
Auto Trait Implementations
impl<G> !RefUnwindSafe for NodeRef<G>
impl<G> !Send for NodeRef<G>
impl<G> !Sync for NodeRef<G>
impl<G> Unpin for NodeRef<G>
impl<G> !UnwindSafe for NodeRef<G>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.