generic_component.rs 745 B

12345678910111213141516171819202122232425262728
  1. //! This example demonstrates how to create a generic component in Dioxus.
  2. //!
  3. //! Generic components can be useful when you want to create a component that renders differently depending on the type
  4. //! of data it receives. In this particular example, we're just using a type that implements `Display` and `PartialEq`,
  5. use dioxus::prelude::*;
  6. use std::fmt::Display;
  7. fn main() {
  8. dioxus::launch(app);
  9. }
  10. fn app() -> Element {
  11. rsx! {
  12. generic_child { data: 0 }
  13. }
  14. }
  15. #[derive(PartialEq, Props, Clone)]
  16. struct GenericChildProps<T: Display + PartialEq + Clone + 'static> {
  17. data: T,
  18. }
  19. fn generic_child<T: Display + PartialEq + Clone>(props: GenericChildProps<T>) -> Element {
  20. rsx! {
  21. div { "{props.data}" }
  22. }
  23. }