1
0

conditional_rendering.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //! Example: Conditional Rendering
  2. //! ------------------------------
  3. //!
  4. //! This example shows how to hide or show elements using conditional rendering.
  5. //!
  6. //! Often times you might want to display a different UI given some sort of condition. This is called "conditonal rendering".
  7. //! In Dioxus, you can perform conditional rendering with optionals or matching.
  8. //!
  9. //! The rsx! and html! macro 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 static Example0: FC<MyProps> = |cx, props| {
  18. cx.render(rsx! {
  19. div {
  20. {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 static Example1: FC<MyProps1> = |cx, props| {
  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. {
  55. props.should_show
  56. .then(|| rsx!(cx, div {"it is false!"}))
  57. .unwrap_or_else(|| rsx!(cx, div {"it is false!"}))
  58. }
  59. }
  60. })
  61. };
  62. /// Matching can be expanded
  63. #[derive(PartialEq)]
  64. pub enum Color {
  65. Green,
  66. Yellow,
  67. Red,
  68. }
  69. #[derive(PartialEq, Props)]
  70. pub struct MyProps2 {
  71. color: Color,
  72. }
  73. pub static Example2: FC<MyProps2> = |cx, props| {
  74. cx.render(rsx! {
  75. div {
  76. {match props.color {
  77. Color::Green => rsx!(cx, div {"it is Green!"}),
  78. Color::Yellow => rsx!(cx, div {"it is Yellow!"}),
  79. Color::Red => rsx!(cx, div {"it is Red!"}),
  80. }}
  81. }
  82. })
  83. };
  84. pub static Example: FC<()> = |cx, props| {
  85. let should_show = use_state(cx, || false);
  86. let mut color_index = use_state(cx, || 0);
  87. let color = match *color_index % 2 {
  88. 2 => Color::Green,
  89. 1 => Color::Yellow,
  90. _ => Color::Red,
  91. };
  92. cx.render(rsx! {
  93. div {
  94. button {
  95. onclick: move |_| should_show.set(!*should_show)
  96. "click to toggle showing the examples"
  97. }
  98. button {
  99. onclick: move |_| color_index += 1
  100. "click to for the enxt color"
  101. }
  102. Example0 { should_show: *should_show }
  103. Example1 { should_show: *should_show }
  104. Example2 { color: color }
  105. }
  106. })
  107. };