pub struct NodeRef<T>(_)
where
T: ElementDescriptor + 'static;
Expand description
Contains a shared reference to a DOM node created while using the view
macro to create your UI.
use leptos::html::Input;
#[component]
pub fn MyComponent(cx: Scope) -> impl IntoView {
let input_ref = create_node_ref::<Input>(cx);
let on_click = move |_| {
let node =
input_ref.get().expect("input_ref should be loaded by now");
// `node` is strongly typed
// it is dereferenced to an `HtmlInputElement` automatically
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§
source§impl<T> NodeRef<T>where
T: ElementDescriptor + 'static,
impl<T> NodeRef<T>where T: ElementDescriptor + 'static,
sourcepub fn new(cx: Scope) -> NodeRef<T>
👎Deprecated: Use create_node_ref
instead of NodeRef::new()
.
pub fn new(cx: Scope) -> NodeRef<T>
create_node_ref
instead of NodeRef::new()
.Creates an empty reference.
sourcepub fn get(&self) -> Option<HtmlElement<T>>where
T: Clone,
pub fn get(&self) -> Option<HtmlElement<T>>where T: Clone,
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)
.
sourcepub fn on_load<F>(self, cx: Scope, f: F)where
T: Clone,
F: FnOnce(HtmlElement<T>) + 'static,
pub fn on_load<F>(self, cx: Scope, f: F)where T: Clone, F: FnOnce(HtmlElement<T>) + 'static,
Runs the provided closure when the NodeRef
has been connected
with it’s HtmlElement
.
Trait Implementations§
source§impl<T> Clone for NodeRef<T>where
T: ElementDescriptor,
impl<T> Clone for NodeRef<T>where T: ElementDescriptor,
impl<T> Copy for NodeRef<T>where T: ElementDescriptor + 'static,
Auto Trait Implementations§
impl<T> !RefUnwindSafe for NodeRef<T>
impl<T> !Send for NodeRef<T>
impl<T> !Sync for NodeRef<T>
impl<T> Unpin for NodeRef<T>where T: Unpin,
impl<T> !UnwindSafe for NodeRef<T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more