component.rs 4.6 KB

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