2021-07-02 05:30:52 +00:00
|
|
|
//! Example: Conditional Rendering
|
|
|
|
//! ------------------------------
|
|
|
|
//!
|
|
|
|
//! This example shows how to hide or show elements using conditional rendering.
|
|
|
|
//!
|
2021-10-24 17:30:36 +00:00
|
|
|
//! Oftentimes you might want to display a different UI given some sort of condition. This is called "conditional rendering".
|
2021-07-02 05:30:52 +00:00
|
|
|
//! In Dioxus, you can perform conditional rendering with optionals or matching.
|
|
|
|
//!
|
2021-10-24 17:30:36 +00:00
|
|
|
//! The rsx! and html! macros accepts anything that is `IntoIter<Item = impl IntoVnode>`. Options and Results both implement
|
2021-07-02 05:30:52 +00:00
|
|
|
//! IntoIter for impl VNode, so you can use option/result for conditional rendering.
|
|
|
|
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
|
|
|
|
// Convert a boolean conditional into a hide/show
|
|
|
|
#[derive(PartialEq, Props)]
|
2021-07-16 20:11:25 +00:00
|
|
|
pub struct MyProps {
|
2021-07-02 05:30:52 +00:00
|
|
|
should_show: bool,
|
|
|
|
}
|
2022-01-02 23:35:38 +00:00
|
|
|
pub fn Example0(cx: Scope<MyProps>) -> Element {
|
2021-07-02 05:30:52 +00:00
|
|
|
cx.render(rsx! {
|
|
|
|
div {
|
2022-01-02 23:35:38 +00:00
|
|
|
cx.props.should_show.then(|| rsx!{
|
2021-07-02 05:30:52 +00:00
|
|
|
h1 { "showing the title!" }
|
2022-01-02 23:35:38 +00:00
|
|
|
})
|
2021-07-02 05:30:52 +00:00
|
|
|
}
|
|
|
|
})
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|
2021-07-02 05:30:52 +00:00
|
|
|
|
|
|
|
// Convert a boolean conditional into an either/or
|
|
|
|
// Because rsx! is lazy (produces a closure), we cannot use it in two branch arms. To use it in matching/branching, we
|
|
|
|
// must render it.
|
|
|
|
//
|
|
|
|
// Dioxus will let you render any `LazyNodes` into a `VNode` with `cx.render`. `rsx!` also supports the `in cx` syntax
|
|
|
|
// which will do essentially the same thing as `cx.render`.
|
|
|
|
//
|
|
|
|
// In short:
|
2021-09-16 17:20:04 +00:00
|
|
|
// `rsx!(cx, ...)` is shorthand for `cx.render(rsx!(...))`
|
2021-07-02 05:30:52 +00:00
|
|
|
#[derive(PartialEq, Props)]
|
2021-07-16 20:11:25 +00:00
|
|
|
pub struct MyProps1 {
|
2021-07-02 05:30:52 +00:00
|
|
|
should_show: bool,
|
|
|
|
}
|
2022-01-02 23:35:38 +00:00
|
|
|
pub fn Example1(cx: Scope<MyProps1>) -> Element {
|
2021-07-02 05:30:52 +00:00
|
|
|
cx.render(rsx! {
|
|
|
|
div {
|
|
|
|
// With matching
|
2022-01-02 23:35:38 +00:00
|
|
|
match props.should_show {
|
2021-07-02 05:30:52 +00:00
|
|
|
true => cx.render(rsx!(div {"it is true!"})),
|
2021-09-16 17:20:04 +00:00
|
|
|
false => rsx!(cx, div {"it is false!"}),
|
2022-01-02 23:35:38 +00:00
|
|
|
},
|
2021-07-02 05:30:52 +00:00
|
|
|
|
|
|
|
// or with just regular conditions
|
2022-01-02 23:35:38 +00:00
|
|
|
if props.should_show {
|
2021-09-16 17:20:04 +00:00
|
|
|
rsx!(cx, div {"it is true!"})
|
2021-07-02 05:30:52 +00:00
|
|
|
} else {
|
2021-09-16 17:20:04 +00:00
|
|
|
rsx!(cx, div {"it is false!"})
|
2022-01-02 23:35:38 +00:00
|
|
|
},
|
2021-07-02 05:30:52 +00:00
|
|
|
|
|
|
|
// or with optional chaining
|
2022-01-02 23:35:38 +00:00
|
|
|
props.should_show
|
2021-10-24 17:30:36 +00:00
|
|
|
.then(|| rsx!(cx, div {"it is true!"}))
|
2021-09-16 17:20:04 +00:00
|
|
|
.unwrap_or_else(|| rsx!(cx, div {"it is false!"}))
|
2021-07-02 05:30:52 +00:00
|
|
|
}
|
|
|
|
})
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|
2021-07-02 05:30:52 +00:00
|
|
|
|
|
|
|
/// Matching can be expanded
|
|
|
|
|
|
|
|
#[derive(PartialEq)]
|
2021-07-16 20:11:25 +00:00
|
|
|
pub enum Color {
|
2021-07-02 05:30:52 +00:00
|
|
|
Green,
|
|
|
|
Yellow,
|
|
|
|
Red,
|
|
|
|
}
|
|
|
|
#[derive(PartialEq, Props)]
|
2021-07-16 20:11:25 +00:00
|
|
|
pub struct MyProps2 {
|
2021-07-02 05:30:52 +00:00
|
|
|
color: Color,
|
|
|
|
}
|
2022-01-02 23:35:38 +00:00
|
|
|
pub fn Example2(cx: Scope<MyProps2>) -> Element {
|
2021-07-02 05:30:52 +00:00
|
|
|
cx.render(rsx! {
|
|
|
|
div {
|
2022-01-02 23:35:38 +00:00
|
|
|
match props.color {
|
2021-09-16 17:20:04 +00:00
|
|
|
Color::Green => rsx!(cx, div {"it is Green!"}),
|
|
|
|
Color::Yellow => rsx!(cx, div {"it is Yellow!"}),
|
|
|
|
Color::Red => rsx!(cx, div {"it is Red!"}),
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|
2021-07-02 05:30:52 +00:00
|
|
|
}
|
|
|
|
})
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|
2021-07-16 20:11:25 +00:00
|
|
|
|
2022-01-02 23:35:38 +00:00
|
|
|
pub fn Example(cx: Scope<()>) -> Element {
|
2021-12-15 20:56:53 +00:00
|
|
|
let should_show = use_state(&cx, || false);
|
|
|
|
let mut color_index = use_state(&cx, || 0);
|
2021-07-16 20:11:25 +00:00
|
|
|
let color = match *color_index % 2 {
|
|
|
|
2 => Color::Green,
|
|
|
|
1 => Color::Yellow,
|
|
|
|
_ => Color::Red,
|
|
|
|
};
|
|
|
|
|
|
|
|
cx.render(rsx! {
|
|
|
|
div {
|
|
|
|
button {
|
2022-01-02 23:35:38 +00:00
|
|
|
onclick: move |_| should_show.set(!*should_show),
|
2021-07-16 20:11:25 +00:00
|
|
|
"click to toggle showing the examples"
|
|
|
|
}
|
|
|
|
button {
|
2022-01-02 23:35:38 +00:00
|
|
|
onclick: move |_| color_index += 1,
|
2021-07-16 20:11:25 +00:00
|
|
|
"click to for the enxt color"
|
|
|
|
}
|
|
|
|
Example0 { should_show: *should_show }
|
|
|
|
Example1 { should_show: *should_show }
|
|
|
|
Example2 { color: color }
|
|
|
|
}
|
|
|
|
})
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|