component.rs 3.9 KB

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