memo.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //! Example: Memoization
  2. //! --------------------
  3. //!
  4. //! This example showcases how memoization works in Dioxus.
  5. //!
  6. //! Memoization is the process in which Dioxus skips diffing child components if their props don't change.
  7. //! In React, components are never memoized unless wrapped in `memo` or configured with `shouldComponentUpdate`.
  8. //!
  9. //! Due to the safety guarantees of Rust, we can automatically memoize components in some circumstances. Whenever a
  10. //! component's properties are valid for the `'static` lifetime, Dioxus will automatically compare the props before
  11. //! diffing the component. If the props don't change (according to PartialEq), the component will not be re-rendered.
  12. //!
  13. //! However, if the props use some generics or borrow from their parent, then Dioxus can't safely supress updates,
  14. //! and is forced to render the child. If you think that this behavior is wrong for your usecase, you can implement
  15. //! the memo method yourself, but beware, doing so is UNSAFE and may cause issues if you do it wrong.
  16. //!
  17. //! If you want to gain that little bit extra performance, consider using global state management, signals, or
  18. //! memoized collections like im-rc which are designed for this use case.
  19. use dioxus::prelude::*;
  20. // By default, components with no props are always memoized.
  21. // A props of () is considered empty.
  22. pub static Example: FC<()> = |cx| {
  23. cx.render(rsx! {
  24. div { "100% memoized!" }
  25. })
  26. };
  27. // These props do not borrow any content, and therefore can be safely memoized.
  28. // However, the parent *must* create a new string on every render.
  29. // Notice how these props implement PartialEq - this is required for 'static props
  30. #[derive(PartialEq, Props)]
  31. pub struct MyProps1 {
  32. name: String,
  33. }
  34. pub static Example1: FC<MyProps1> = |cx| {
  35. cx.render(rsx! {
  36. div { "100% memoized! {cx.name}" }
  37. })
  38. };
  39. // These props do not borrow any content, and therefore can be safely memoized.
  40. // In contrast with the `String` example, these props use `Rc<str>` which operates similar to strings in JavaScript.
  41. // These strings cannot be modified, but may be cheaply shared in many places without issue.
  42. #[derive(PartialEq, Props)]
  43. pub struct MyProps2 {
  44. name: std::rc::Rc<str>,
  45. }
  46. pub static Example2: FC<MyProps2> = |cx| {
  47. cx.render(rsx! {
  48. div { "100% memoized! {cx.name}" }
  49. })
  50. };
  51. // These props *do* borrow any content, and therefore cannot be safely memoized!.
  52. #[derive(PartialEq, Props)]
  53. pub struct MyProps3<'a> {
  54. name: &'a str,
  55. }
  56. // We need to manually specify a lifetime that ensures props and scope (the component's state) share the same lifetime.
  57. // Using the `pub static Example: FC<()>` pattern _will_ specify a lifetime, but that lifetime will be static which might
  58. // not exactly be what you want
  59. fn Example3<'a>(cx: Context<'a, MyProps3<'a>>) -> VNode {
  60. cx.render(rsx! {
  61. div { "Not memoized! {cx.name}" }
  62. })
  63. }
  64. // These props *do* borrow any content, and therefore cannot be safely memoized!.
  65. // However, they cannot be compared, so we don't need the PartialEq flag.
  66. #[derive(Props)]
  67. pub struct MyProps4<'a> {
  68. onhandle: &'a dyn Fn(),
  69. }
  70. // We need to manually specify a lifetime that ensures props and scope (the component's state) share the same lifetime.
  71. fn Example4<'a>(cx: Context<'a, MyProps4<'a>>) -> VNode {
  72. cx.render(rsx! {
  73. div { "Not memoized!", onclick: move |_| (cx.onhandle)() }
  74. })
  75. }