spreadpattern.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. pub static Example: FC<()> = |(cx, props)| {
  11. let props = MyProps {
  12. count: 0,
  13. live: true,
  14. name: "Dioxus",
  15. };
  16. cx.render(rsx! {
  17. Example1 { ..props, count: 10, div {"child"} }
  18. })
  19. };
  20. #[derive(PartialEq, Props)]
  21. pub struct MyProps {
  22. count: u32,
  23. live: bool,
  24. name: &'static str,
  25. }
  26. pub static Example1: FC<MyProps> = |cx, MyProps { count, live, name }| {
  27. cx.render(rsx! {
  28. div {
  29. h1 { "Hello, {name}"}
  30. h3 {"Are we alive? {live}"}
  31. p {"Count is {count}"}
  32. { cx.children() }
  33. }
  34. })
  35. };