You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// The onmounted event is fired when the element is first added to the DOM. This event gives you a [`MountedData`] object and lets you interact with the raw DOM element.
223
-
///
224
-
/// This event is fired once per element. If you need to access the element multiple times, you can store the [`MountedData`] object in a [`use_signal`](https://docs.rs/dioxus-hooks/latest/dioxus_hooks/fn.use_signal.html) hook and use it as needed.
225
-
///
226
-
/// # Examples
227
-
///
228
-
/// ```rust, no_run
229
-
/// # use dioxus::prelude::*;
230
-
/// fn App() -> Element {
231
-
/// let mut header_element = use_signal(|| None);
232
-
///
233
-
/// rsx! {
234
-
/// div {
235
-
/// h1 {
236
-
/// // The onmounted event will run the first time the h1 element is mounted
/// // When you click the button, if the header element has been mounted, we scroll to that element
247
-
/// onclick: move |_| async move {
248
-
/// if let Some(header) = header_element.cloned() {
249
-
/// let _ = header.scroll_to(ScrollBehavior::Smooth).await;
250
-
/// }
251
-
/// },
252
-
/// "Scroll to top"
253
-
/// }
254
-
/// }
255
-
/// }
256
-
/// }
257
-
/// ```
258
-
///
259
-
/// The `MountedData` struct contains cross platform APIs that work on the desktop, mobile, liveview and web platforms. For the web platform, you can also downcast the `MountedData` event to the `web-sys::Element` type for more web specific APIs:
260
-
///
261
-
/// ```rust, ignore
262
-
/// use dioxus::prelude::*;
263
-
/// use dioxus_web::WebEventExt; // provides [`as_web_event()`] method
264
-
///
265
-
/// fn App() -> Element {
266
-
/// rsx! {
267
-
/// div {
268
-
/// id: "some-id",
269
-
/// onmounted: move |element| {
270
-
/// // You can use the web_event trait to downcast the element to a web specific event. For the mounted event, this will be a web_sys::Element
/// The onmounted event is fired when the element is first added to the DOM. This event gives you a [`MountedData`] object and lets you interact with the raw DOM element.
307
+
///
308
+
/// This event is fired once per element. If you need to access the element multiple times, you can store the [`MountedData`] object in a [`use_signal`](https://docs.rs/dioxus-hooks/latest/dioxus_hooks/fn.use_signal.html) hook and use it as needed.
309
+
///
310
+
/// You can optionally return a cleanup closure that will be called when the element is removed from the DOM:
311
+
///
312
+
/// # Examples
313
+
///
314
+
/// ## Basic usage (no cleanup)
315
+
/// ```rust, no_run
316
+
/// # use dioxus::prelude::*;
317
+
/// fn App() -> Element {
318
+
/// let mut header_element = use_signal(|| None);
319
+
///
320
+
/// rsx! {
321
+
/// div {
322
+
/// h1 {
323
+
/// // The onmounted event will run the first time the h1 element is mounted
/// // When you click the button, if the header element has been mounted, we scroll to that element
334
+
/// onclick: move |_| async move {
335
+
/// if let Some(header) = header_element.cloned() {
336
+
/// let _ = header.scroll_to(ScrollBehavior::Smooth).await;
337
+
/// }
338
+
/// },
339
+
/// "Scroll to top"
340
+
/// }
341
+
/// }
342
+
/// }
343
+
/// }
344
+
/// ```
345
+
///
346
+
/// ## With cleanup closure
347
+
/// ```rust, no_run
348
+
/// # use dioxus::prelude::*;
349
+
/// fn App() -> Element {
350
+
/// let mut cleanup_called = use_signal(|| false);
351
+
///
352
+
/// rsx! {
353
+
/// div {
354
+
/// onmounted: move |_| {
355
+
/// // Return a cleanup closure that runs when the element is removed
356
+
/// move || {
357
+
/// cleanup_called.set(true);
358
+
/// }
359
+
/// },
360
+
/// "Element with cleanup"
361
+
/// }
362
+
/// }
363
+
/// }
364
+
/// ```
365
+
///
366
+
/// The `MountedData` struct contains cross platform APIs that work on the desktop, mobile, liveview and web platforms. For the web platform, you can also downcast the `MountedData` event to the `web-sys::Element` type for more web specific APIs:
367
+
///
368
+
/// ```rust, ignore
369
+
/// use dioxus::prelude::*;
370
+
/// use dioxus_web::WebEventExt; // provides [`as_web_event()`] method
371
+
///
372
+
/// fn App() -> Element {
373
+
/// rsx! {
374
+
/// div {
375
+
/// id: "some-id",
376
+
/// onmounted: move |element| {
377
+
/// // You can use the web_event trait to downcast the element to a web specific event. For the mounted event, this will be a web_sys::Element
0 commit comments