dioxus/examples/core_reference/memo.rs

85 lines
3.4 KiB
Rust
Raw Normal View History

2021-07-02 05:30:52 +00:00
//! Example: Memoization
//! --------------------
//!
//! This example showcases how memoization works in Dioxus.
//!
//! Memoization is the process in which Dioxus skips diffing child components if their props don't change.
//! In React, components are never memoized unless wrapped in `memo` or configured with `shouldComponentUpdate`.
//!
//! Due to the safety guarantees of Rust, we can automatically memoize components in some circumstances. Whenever a
//! component's properties are valid for the `'static` lifetime, Dioxus will automatically compare the props before
//! diffing the component. If the props don't change (according to PartialEq), the component will not be re-rendered.
//!
//! However, if the props use some generics or borrow from their parent, then Dioxus can't safely supress updates,
//! and is forced to render the child. If you think that this behavior is wrong for your usecase, you can implement
//! the memo method yourself, but beware, doing so is UNSAFE and may cause issues if you do it wrong.
//!
//! If you want to gain that little bit extra performance, consider using global state management, signals, or
//! memoized collections like im-rc which are designed for this use case.
use dioxus::prelude::*;
// By default, components with no props are always memoized.
// A props of () is considered empty.
pub fn Example(cx: Scope) -> Element {
2021-07-02 05:30:52 +00:00
cx.render(rsx! {
div { "100% memoized!" }
})
}
2021-07-02 05:30:52 +00:00
// These props do not borrow any content, and therefore can be safely memoized.
// However, the parent *must* create a new string on every render.
// Notice how these props implement PartialEq - this is required for 'static props
#[derive(PartialEq, Props)]
2021-07-16 20:11:25 +00:00
pub struct MyProps1 {
2021-07-02 05:30:52 +00:00
name: String,
}
pub fn Example1(cx: Scope<MyProps1>) -> Element {
2021-07-02 05:30:52 +00:00
cx.render(rsx! {
div { "100% memoized! {cx.props.name}" }
2021-07-02 05:30:52 +00:00
})
}
2021-07-02 05:30:52 +00:00
// These props do not borrow any content, and therefore can be safely memoized.
// In contrast with the `String` example, these props use `Rc<str>` which operates similar to strings in JavaScript.
// These strings cannot be modified, but may be cheaply shared in many places without issue.
#[derive(PartialEq, Props)]
2021-07-16 20:11:25 +00:00
pub struct MyProps2 {
2021-07-02 05:30:52 +00:00
name: std::rc::Rc<str>,
}
pub fn Example2(cx: Scope<MyProps2>) -> Element {
2021-07-02 05:30:52 +00:00
cx.render(rsx! {
div { "100% memoized! {cx.props.name}" }
2021-07-02 05:30:52 +00:00
})
}
2021-07-02 05:30:52 +00:00
// These props *do* borrow any content, and therefore cannot be safely memoized!.
#[derive(PartialEq, Props)]
2021-07-16 20:11:25 +00:00
pub struct MyProps3<'a> {
2021-07-02 05:30:52 +00:00
name: &'a str,
}
// We need to manually specify a lifetime that ensures props and scope (the component's state) share the same lifetime.
// Using the `pub fn Example(cx: Scope): Component` pattern _will_ specify a lifetime, but that lifetime will be static which might
2021-07-02 05:30:52 +00:00
// not exactly be what you want
2021-12-29 04:48:25 +00:00
fn Example3(cx: Scope<'a, MyProps3<'a>>) -> DomTree {
2021-07-02 05:30:52 +00:00
cx.render(rsx! {
div { "Not memoized! {cx.props.name}" }
2021-07-02 05:30:52 +00:00
})
}
// These props *do* borrow any content, and therefore cannot be safely memoized!.
// However, they cannot be compared, so we don't need the PartialEq flag.
#[derive(Props)]
2021-07-16 20:11:25 +00:00
pub struct MyProps4<'a> {
2021-07-02 05:30:52 +00:00
onhandle: &'a dyn Fn(),
}
// We need to manually specify a lifetime that ensures props and scope (the component's state) share the same lifetime.
2021-12-29 04:48:25 +00:00
fn Example4<'a>(cx: Scope<'a, MyProps4<'a>>) -> DomTree {
2021-07-02 05:30:52 +00:00
cx.render(rsx! {
2021-09-21 17:42:52 +00:00
div { "Not memoized!", onclick: move |_| (props.onhandle)() }
2021-07-02 05:30:52 +00:00
})
}