2024-02-14 21:48:58 +00:00
|
|
|
//! A few ways of mapping elements into rsx! syntax
|
|
|
|
//!
|
|
|
|
//! Rsx allows anything that's an iterator where the output type implements Into<Element>, so you can use any of the following:
|
|
|
|
|
2022-09-13 05:49:04 +00:00
|
|
|
use dioxus::prelude::*;
|
|
|
|
|
|
|
|
fn main() {
|
2024-05-28 20:05:55 +00:00
|
|
|
launch(app);
|
2022-09-13 05:49:04 +00:00
|
|
|
}
|
|
|
|
|
2024-01-14 04:51:37 +00:00
|
|
|
fn app() -> Element {
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx!(
|
2022-11-17 06:20:41 +00:00
|
|
|
div {
|
2022-11-16 07:22:41 +00:00
|
|
|
// Use Map directly to lazily pull elements
|
2024-07-03 00:09:57 +00:00
|
|
|
{(0..10).map(|f| rsx! { "{f}" })}
|
2022-11-16 07:31:23 +00:00
|
|
|
|
2022-11-16 07:22:41 +00:00
|
|
|
// Collect into an intermediate collection if necessary, and call into_iter
|
2024-01-11 03:33:34 +00:00
|
|
|
{["a", "b", "c", "d", "e", "f"]
|
2022-11-16 07:22:41 +00:00
|
|
|
.into_iter()
|
2024-01-16 19:18:46 +00:00
|
|
|
.map(|f| rsx! { "{f}" })
|
2022-11-16 07:22:41 +00:00
|
|
|
.collect::<Vec<_>>()
|
2024-07-03 00:09:57 +00:00
|
|
|
.into_iter()}
|
2022-11-16 07:22:41 +00:00
|
|
|
|
|
|
|
// Use optionals
|
2024-07-03 00:09:57 +00:00
|
|
|
{Some(rsx! { "Some" })}
|
2022-11-17 06:10:50 +00:00
|
|
|
|
2022-11-18 06:55:46 +00:00
|
|
|
// use a for loop where the body itself is RSX
|
2022-11-17 06:20:41 +00:00
|
|
|
for name in 0..10 {
|
2024-02-01 00:26:12 +00:00
|
|
|
div { "{name}" }
|
2022-11-17 06:20:41 +00:00
|
|
|
}
|
2022-11-17 06:10:50 +00:00
|
|
|
|
2022-11-17 06:20:41 +00:00
|
|
|
// Or even use an unterminated conditional
|
|
|
|
if true {
|
2024-01-11 03:33:34 +00:00
|
|
|
"hello world!"
|
2022-11-17 06:10:50 +00:00
|
|
|
}
|
2022-11-16 07:22:41 +00:00
|
|
|
}
|
2024-01-14 05:12:21 +00:00
|
|
|
)
|
2022-09-13 05:49:04 +00:00
|
|
|
}
|