component.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //! This file handles the supporting infrastructure for the `Component` trait and `Properties` which makes it possible
  2. //! for components to be used within Nodes.
  3. //!
  4. //! Note - using the builder pattern does not required the Properties trait to be implemented - the only thing that matters is
  5. //! if the type supports PartialEq. The Properties trait is used by the rsx! and html! macros to generate the type-safe builder
  6. //! that ensures compile-time required and optional fields on cx.
  7. use crate::innerlude::{Context, DomTree, LazyNodes, FC};
  8. /// A component is a wrapper around a Context and some Props that share a lifetime
  9. ///
  10. ///
  11. /// # Example
  12. ///
  13. /// With memoized state:
  14. /// ```rust
  15. /// struct State {}
  16. ///
  17. /// fn Example((cx, props): Component<State>) -> DomTree {
  18. /// // ...
  19. /// }
  20. /// ```
  21. ///
  22. /// With borrowed state:
  23. /// ```rust
  24. /// struct State<'a> {
  25. /// name: &'a str
  26. /// }
  27. ///
  28. /// fn Example<'a>((cx, props): Component<'a, State>) -> DomTree<'a> {
  29. /// // ...
  30. /// }
  31. /// ```
  32. ///
  33. /// With owned state as a closure:
  34. /// ```rust
  35. /// static Example: FC<()> = |(cx, props)| {
  36. /// // ...
  37. /// };
  38. /// ```
  39. ///
  40. pub type Component<'a, T> = (Context<'a>, &'a T);
  41. /// Create inline fragments using Component syntax.
  42. ///
  43. /// Fragments capture a series of children without rendering extra nodes.
  44. ///
  45. /// # Example
  46. ///
  47. /// ```rust
  48. /// rsx!{
  49. /// Fragment { key: "abc" }
  50. /// }
  51. /// ```
  52. ///
  53. /// # Details
  54. ///
  55. /// Fragments are incredibly useful when necessary, but *do* add cost in the diffing phase.
  56. /// Try to avoid nesting fragments if you can. There is no protection against infinitely nested fragments.
  57. ///
  58. /// This function defines a dedicated `Fragment` component that can be used to create inline fragments in the RSX macro.
  59. ///
  60. /// You want to use this free-function when your fragment needs a key and simply returning multiple nodes from rsx! won't cut it.
  61. ///
  62. #[allow(non_upper_case_globals, non_snake_case)]
  63. pub fn Fragment((cx, _): Component<()>) -> DomTree {
  64. cx.render(LazyNodes::new(|f| f.fragment_from_iter(cx.children())))
  65. }
  66. /// Every "Props" used for a component must implement the `Properties` trait. This trait gives some hints to Dioxus
  67. /// on how to memoize the props and some additional optimizations that can be made. We strongly encourage using the
  68. /// derive macro to implement the `Properties` trait automatically as guarantee that your memoization strategy is safe.
  69. ///
  70. /// If your props are 'static, then Dioxus will require that they also be PartialEq for the derived memoize strategy. However,
  71. /// if your props borrow data, then the memoization strategy will simply default to "false" and the PartialEq will be ignored.
  72. /// This tends to be useful when props borrow something that simply cannot be compared (IE a reference to a closure);
  73. ///
  74. /// By default, the memoization strategy is very conservative, but can be tuned to be more aggressive manually. However,
  75. /// this is only safe if the props are 'static - otherwise you might borrow references after-free.
  76. ///
  77. /// We strongly suggest that any changes to memoization be done at the "PartialEq" level for 'static props. Additionally,
  78. /// we advise the use of smart pointers in cases where memoization is important.
  79. ///
  80. /// ## Example
  81. ///
  82. /// For props that are 'static:
  83. /// ```rust ignore
  84. /// #[derive(Props, PartialEq)]
  85. /// struct MyProps {
  86. /// data: String
  87. /// }
  88. /// ```
  89. ///
  90. /// For props that borrow:
  91. ///
  92. /// ```rust ignore
  93. /// #[derive(Props)]
  94. /// struct MyProps<'a >{
  95. /// data: &'a str
  96. /// }
  97. /// ```
  98. pub trait Properties: Sized {
  99. type Builder;
  100. const IS_STATIC: bool;
  101. fn builder() -> Self::Builder;
  102. /// Memoization can only happen if the props are valid for the 'static lifetime
  103. ///
  104. /// # Safety
  105. /// The user must know if their props are static, but if they make a mistake, UB happens
  106. /// Therefore it's unsafe to memoize.
  107. unsafe fn memoize(&self, other: &Self) -> bool;
  108. }
  109. impl Properties for () {
  110. type Builder = EmptyBuilder;
  111. const IS_STATIC: bool = true;
  112. fn builder() -> Self::Builder {
  113. EmptyBuilder {}
  114. }
  115. unsafe fn memoize(&self, _other: &Self) -> bool {
  116. true
  117. }
  118. }
  119. // We allow components to use the () generic parameter if they have no props. This impl enables the "build" method
  120. // that the macros use to anonymously complete prop construction.
  121. pub struct EmptyBuilder;
  122. impl EmptyBuilder {
  123. #[inline]
  124. pub fn build(self) {}
  125. }
  126. /// This utility function launches the builder method so rsx! and html! macros can use the typed-builder pattern
  127. /// to initialize a component's props.
  128. pub fn fc_to_builder<T: Properties>(_: FC<T>) -> T::Builder {
  129. T::builder()
  130. }