dioxus/examples/core_reference/fragments.rs

52 lines
1.3 KiB
Rust
Raw Normal View History

2021-07-02 05:30:52 +00:00
//! Example: Fragments
//! ------------------
//!
//! Dioxus can return multiple elements without a container through the use of the VNode called a "Fragment". Fragments do not
//! have a mounted root and are inserted inline with their siblings. There are three ways of creating fragments as outlined
//! in the examples below:
//! - By returning multiple elements in Rsx!
//! - By using the `Fragment` component
//! - By using the fragment() method on the node factory
use dioxus::prelude::*;
// Returning multiple elements with rsx! or html!
2021-12-29 04:48:25 +00:00
static App1: Component = |cx| {
2021-07-02 05:30:52 +00:00
cx.render(rsx! {
h1 { }
h2 { }
h3 { }
})
};
// Using the Fragment component
2021-12-29 04:48:25 +00:00
static App2: Component = |cx| {
2021-07-02 05:30:52 +00:00
cx.render(rsx! {
Fragment {
div {}
div {}
"asd"
}
})
};
// Using the `fragment` method on the NodeFactory
2021-12-29 04:48:25 +00:00
static App3: Component = |cx| {
2021-07-11 22:39:45 +00:00
cx.render(LazyNodes::new(move |fac| {
fac.fragment_from_iter([
fac.text(format_args!("A")),
fac.text(format_args!("B")),
fac.text(format_args!("A")),
fac.text(format_args!("B")),
])
2021-07-02 05:30:52 +00:00
}))
};
2021-07-16 20:11:25 +00:00
2021-12-29 04:48:25 +00:00
pub static Example: Component = |cx| {
2021-07-16 20:11:25 +00:00
cx.render(rsx! {
App1 {}
App2 {}
App3 {}
})
};