conditional_rendering.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //! Example: Conditional Rendering
  2. //! ------------------------------
  3. //!
  4. //! This example shows how to hide or show elements using conditional rendering.
  5. //!
  6. //! Oftentimes you might want to display a different UI given some sort of condition. This is called "conditional rendering".
  7. //! In Dioxus, you can perform conditional rendering with optionals or matching.
  8. //!
  9. //! The rsx! and html! macros accepts anything that is `IntoIter<Item = impl IntoVnode>`. Options and Results both implement
  10. //! IntoIter for impl VNode, so you can use option/result for conditional rendering.
  11. use dioxus::prelude::*;
  12. // Convert a boolean conditional into a hide/show
  13. #[derive(PartialEq, Props)]
  14. pub struct MyProps {
  15. should_show: bool,
  16. }
  17. pub fn Example0(cx: Scope<MyProps>) -> Element {
  18. cx.render(rsx! {
  19. div {
  20. cx.props.should_show.then(|| rsx!{
  21. h1 { "showing the title!" }
  22. })
  23. }
  24. })
  25. }
  26. // Convert a boolean conditional into an either/or
  27. // Because rsx! is lazy (produces a closure), we cannot use it in two branch arms. To use it in matching/branching, we
  28. // must render it.
  29. //
  30. // Dioxus will let you render any `LazyNodes` into a `VNode` with `cx.render`. `rsx!` also supports the `in cx` syntax
  31. // which will do essentially the same thing as `cx.render`.
  32. //
  33. // In short:
  34. // `rsx!(cx, ...)` is shorthand for `cx.render(rsx!(...))`
  35. #[derive(PartialEq, Props)]
  36. pub struct MyProps1 {
  37. should_show: bool,
  38. }
  39. pub fn Example1(cx: Scope<MyProps1>) -> Element {
  40. cx.render(rsx! {
  41. div {
  42. // With matching
  43. match props.should_show {
  44. true => cx.render(rsx!(div {"it is true!"})),
  45. false => rsx!(cx, div {"it is false!"}),
  46. },
  47. // or with just regular conditions
  48. if props.should_show {
  49. rsx!(cx, div {"it is true!"})
  50. } else {
  51. rsx!(cx, div {"it is false!"})
  52. },
  53. // or with optional chaining
  54. props.should_show
  55. .then(|| rsx!(cx, div {"it is true!"}))
  56. .unwrap_or_else(|| rsx!(cx, div {"it is false!"}))
  57. }
  58. })
  59. }
  60. /// Matching can be expanded
  61. #[derive(PartialEq)]
  62. pub enum Color {
  63. Green,
  64. Yellow,
  65. Red,
  66. }
  67. #[derive(PartialEq, Props)]
  68. pub struct MyProps2 {
  69. color: Color,
  70. }
  71. pub fn Example2(cx: Scope<MyProps2>) -> Element {
  72. cx.render(rsx! {
  73. div {
  74. match props.color {
  75. Color::Green => rsx!(cx, div {"it is Green!"}),
  76. Color::Yellow => rsx!(cx, div {"it is Yellow!"}),
  77. Color::Red => rsx!(cx, div {"it is Red!"}),
  78. }
  79. }
  80. })
  81. }
  82. pub fn Example(cx: Scope<()>) -> Element {
  83. let should_show = use_state(&cx, || false);
  84. let mut color_index = use_state(&cx, || 0);
  85. let color = match *color_index % 2 {
  86. 2 => Color::Green,
  87. 1 => Color::Yellow,
  88. _ => Color::Red,
  89. };
  90. cx.render(rsx! {
  91. div {
  92. button {
  93. onclick: move |_| should_show.set(!*should_show),
  94. "click to toggle showing the examples"
  95. }
  96. button {
  97. onclick: move |_| color_index += 1,
  98. "click to for the enxt color"
  99. }
  100. Example0 { should_show: *should_show }
  101. Example1 { should_show: *should_show }
  102. Example2 { color: color }
  103. }
  104. })
  105. }