|
| 1 | +```python exec |
| 2 | +import reflex as rx |
| 3 | +from pcweb.pages.docs import styling |
| 4 | +``` |
| 5 | + |
| 6 | +# Memo |
| 7 | + |
| 8 | +The `memo` decorator is used to optimize component rendering by memoizing components that don't need to be re-rendered. This is particularly useful for expensive components that depend on specific props and don't need to be re-rendered when other state changes. |
| 9 | + |
| 10 | +## Basic Usage |
| 11 | + |
| 12 | +When you wrap a component function with `@rx.memo`, the component will only re-render when its props change. This helps improve performance by preventing unnecessary re-renders. |
| 13 | + |
| 14 | +```python |
| 15 | +@rx.memo |
| 16 | +def expensive_component(label): |
| 17 | + return rx.vstack( |
| 18 | + rx.heading(label, size="lg"), |
| 19 | + rx.text("This component only re-renders when props change!"), |
| 20 | + rx.divider(), |
| 21 | + ) |
| 22 | + |
| 23 | +def index(): |
| 24 | + return rx.vstack( |
| 25 | + rx.heading("Memo Example"), |
| 26 | + rx.text("Count: 0"), |
| 27 | + rx.button("Increment"), |
| 28 | + rx.divider(), |
| 29 | + expensive_component(label="Memoized Component"), |
| 30 | + spacing="4", |
| 31 | + padding="4", |
| 32 | + border_radius="md", |
| 33 | + border="1px solid #eaeaea", |
| 34 | + ) |
| 35 | +``` |
| 36 | + |
| 37 | +In this example, the `expensive_component` will only re-render when the `label` prop changes, not when the `count` state changes. |
| 38 | + |
| 39 | +## With Event Handlers |
| 40 | + |
| 41 | +You can also use `rx.memo` with components that have event handlers: |
| 42 | + |
| 43 | +```python |
| 44 | +@rx.memo |
| 45 | +def my_button(text, on_click=None): |
| 46 | + return rx.button(text, on_click=on_click) |
| 47 | + |
| 48 | +def index(): |
| 49 | + return rx.vstack( |
| 50 | + rx.text("Clicks: 0"), |
| 51 | + my_button(text="Click me"), |
| 52 | + spacing="4", |
| 53 | + ) |
| 54 | +``` |
| 55 | + |
| 56 | +## With State Variables |
| 57 | + |
| 58 | +When used with state variables, memoized components will only re-render when the specific state variables they depend on change: |
| 59 | + |
| 60 | +```python |
| 61 | +class AppState(rx.State): |
| 62 | + name: str = "World" |
| 63 | + count: int = 0 |
| 64 | + |
| 65 | + def increment(self): |
| 66 | + self.count += 1 |
| 67 | + |
| 68 | + def set_name(self, name): |
| 69 | + self.name = name |
| 70 | + |
| 71 | +@rx.memo |
| 72 | +def greeting(name): |
| 73 | + return rx.heading("Hello, " + name) |
| 74 | + |
| 75 | +def index(): |
| 76 | + return rx.vstack( |
| 77 | + greeting(name=AppState.name), |
| 78 | + rx.text("Count: " + str(AppState.count)), |
| 79 | + rx.button("Increment Count", on_click=AppState.increment), |
| 80 | + rx.input( |
| 81 | + placeholder="Enter your name", |
| 82 | + on_change=AppState.set_name, |
| 83 | + value=AppState.name, |
| 84 | + ), |
| 85 | + spacing="4", |
| 86 | + ) |
| 87 | +``` |
| 88 | + |
| 89 | +## Performance Considerations |
| 90 | + |
| 91 | +Use `rx.memo` for: |
| 92 | +- Components with expensive rendering logic |
| 93 | +- Components that render the same result given the same props |
| 94 | +- Components that re-render too often due to parent component updates |
| 95 | + |
| 96 | +Avoid using `rx.memo` for: |
| 97 | +- Simple components where the memoization overhead might exceed the performance gain |
| 98 | +- Components that almost always receive different props on re-render |
0 commit comments