spreadpattern.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //! Example: Spread pattern for Components
  2. //! --------------------------------------
  3. //!
  4. //! Dioxus supports the "spread" pattern for manually building a components properties. This is useful when props
  5. //! are passed down from a parent, or it's more ergonomic to construct props from outside the rsx! macro.
  6. //!
  7. //! To use the spread pattern, simply pass ".." followed by a Rust epxression. This pattern also supports overriding
  8. //! values, using the manual props as the base and then modifying fields specified with non-spread attributes.
  9. use dioxus::prelude::*;
  10. fn main() {}
  11. static App: FC<()> = |cx| {
  12. let props = MyProps {
  13. count: 0,
  14. live: true,
  15. name: "Dioxus",
  16. };
  17. cx.render(rsx! {
  18. Example { ..props, count: 10, div {"child"} }
  19. })
  20. };
  21. #[derive(PartialEq, Props)]
  22. struct MyProps {
  23. count: u32,
  24. live: bool,
  25. name: &'static str,
  26. }
  27. static Example: FC<MyProps> = |cx| {
  28. cx.render(rsx! {
  29. div {
  30. h1 { "Hello, {cx.name}"}
  31. h3 {"Are we alive? {cx.live}"}
  32. p {"Count is {cx.count}"}
  33. { cx.children() }
  34. }
  35. })
  36. };