component.rs 3.9 KB

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