|
| 1 | +# State and Hooks Cheat Sheet |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + <!-- flowchart TD |
| 6 | + A("**Start**<br/>Which scope does your state or utility belong to?") |
| 7 | + |
| 8 | + A --> B["Local<br/>(per component)"] |
| 9 | + A --> C["Shared<br/>(Context)"] |
| 10 | + A --> D["Persistent<br/>(Storage)"] |
| 11 | + A --> E["Global<br/>(Truly app-wide)"] |
| 12 | + A --> F["Route-based"] |
| 13 | + |
| 14 | + B --> LH("When does the code need to run?") |
| 15 | + |
| 16 | + LH --> Init["First Render (Non-Reactive)"] |
| 17 | + LH --> BeforeRender["Before Every<br/>Subsequent Render"] |
| 18 | + LH --> Render["Render Phase (While Component Is Alive)"] |
| 19 | + LH --> Post["Post Renders"] |
| 20 | + LH --> Clean["Cleanup"] |
| 21 | + |
| 22 | + %% First Render |
| 23 | + subgraph FR_Sync ["Sync"] |
| 24 | + L14["**use_hook**<br/>Run on the first use of the hook, which is typically the initial render"] |
| 25 | + L16["**use_hook_with_cleanup**<br/>Use a hook with a cleanup function that runs when component is dropped"] |
| 26 | + U1["**use_debounce**<br/>Calls a function only after provided duration has passed (dioxus_sdk)"] |
| 27 | + U2["**use_interval**<br/>Repeatedly calls a function every certain period (dioxus_sdk)"] |
| 28 | + L18["**use_server_cached**<br/>Runs a function on the server (or client if server is not enabled) and sends result to client. Caches the value on the client"] |
| 29 | + end |
| 30 | + |
| 31 | + subgraph FR_Async ["Async"] |
| 32 | + U3["**use_channel/use_listen_channel**<br/>Create and listen to channels for communication between components (dioxus_sdk)"] |
| 33 | + end |
| 34 | + |
| 35 | + Init --> FR_Sync |
| 36 | + Init --> FR_Async |
| 37 | + |
| 38 | + %% Before Every Render |
| 39 | + subgraph BR_Sync ["Sync"] |
| 40 | + L19["**use_before_render**<br/>Register a function to run before every subsequent render"] |
| 41 | + end |
| 42 | + |
| 43 | + BeforeRender --> BR_Sync |
| 44 | + |
| 45 | + %% Render phase |
| 46 | + subgraph RP_Sync ["Sync"] |
| 47 | + L7["**use_callback**<br/>Keeps a callback up to date for all references to the handle"] |
| 48 | + L1["**use_signal**<br/>Basic local state, triggers re-renders on write, does not subscribe to other signals"] |
| 49 | + L1b["**use_signal_sync**<br/>Thread-safe signal - Send + Sync"] |
| 50 | + L3["**use_memo**<br/>Derived state (memoized), composes signals"] |
| 51 | + L10["**use_set_compare/use_set_compare_equal**<br/>Efficient value change tracking"] |
| 52 | + end |
| 53 | + |
| 54 | + subgraph RP_Both ["Sync & Async"] |
| 55 | + L8["**use_reactive**<br/>Creates a closure (async/sync) to track 'non-reactive' data"] |
| 56 | + end |
| 57 | + |
| 58 | + subgraph RP_Async ["Async"] |
| 59 | + L5["**use_future**<br/>Run an async task once"] |
| 60 | + L6["**use_coroutine**<br/>Stream-based concurrency through channels"] |
| 61 | + L4["**use_resource**<br/>Async derived state"] |
| 62 | + L15["**use_server_future**<br/>Async derived state that runs on the server and caches on the client"] |
| 63 | + end |
| 64 | + |
| 65 | + Render --> RP_Sync |
| 66 | + Render --> RP_Both |
| 67 | + Render --> RP_Async |
| 68 | + |
| 69 | + %% Post render |
| 70 | + subgraph PR_Sync ["Sync"] |
| 71 | + L2["**use_effect**<br/>Side effects after render, composes signals"] |
| 72 | + L9["**use_hook_did_run**<br/>Lifecycle check if this hook has been called on the latest render"] |
| 73 | + L17["**use_after_render**<br/>Push a function to be run after the next render"] |
| 74 | + end |
| 75 | + |
| 76 | + Post --> PR_Sync |
| 77 | + |
| 78 | + %% Cleanup |
| 79 | + subgraph CL_Sync ["Sync"] |
| 80 | + L13["**use_drop**<br/>Callback invoked when component is dropped"] |
| 81 | + end |
| 82 | + |
| 83 | + Clean --> CL_Sync |
| 84 | + |
| 85 | + %% Context |
| 86 | + subgraph CT_Sync ["Sync"] |
| 87 | + C1["**use_context_provider**<br/>Provides data to any child"] |
| 88 | + C2["**use_context**<br/>Consume provided data"] |
| 89 | + C3["**try_use_context**<br/>Like use_context but returns None if missing"] |
| 90 | + C4["**use_root_context**<br/>Like use_context except creates context at root if missing"] |
| 91 | + C5["**use_coroutine_handle**<br/>Obtain handle to a context-provided coroutine"] |
| 92 | + |
| 93 | + subgraph CT_Utils ["Utils"] |
| 94 | + C6["**use_clipboard**<br/>Access the clipboard (dioxus_sdk)"] |
| 95 | + C7["**use_window_size**<br/>Initial window size will be returned with this hook and updated continously as the window is resized (dioxus_sdk)"] |
| 96 | + C8["**use_geolocation**<br/>Provides the latest geocoordinates. Good for navigation-type apps (dioxus_sdk)"] |
| 97 | + C9["**use_system_theme**<br/>Initial theme will be returned and updated if the system theme changes (dioxus_sdk)"] |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + C --> CT_Sync |
| 102 | + |
| 103 | + %% Persistent |
| 104 | + subgraph PS_Sync ["Sync"] |
| 105 | + P1["**use_persistent**<br/>Store data across application reloads as either local storage or a file storage (dioxus_sdk)"] |
| 106 | + P2["**use_storage**<br/>Like use_persistent except the storage location is generic, which may be useful for custom implementations (dioxus_sdk)"] |
| 107 | + P3["**use_synced_storage**<br/>Store data that persists and syncs across all app sessions (dioxus_sdk)"] |
| 108 | + P4["**use_singleton_persistent**<br/>Persistent storage shared for calls from same line (dioxus_sdk)"] |
| 109 | + P5["**use_storage_entry**<br/>Creates a StorageEntry with latest value from storage or init if does not exist (dioxus_sdk)"] |
| 110 | + P6["**use_synced_storage_entry**<br/>Creates a StorageEntry with updates subscription (dioxus_sdk)"] |
| 111 | + end |
| 112 | + |
| 113 | + D --> PS_Sync |
| 114 | + |
| 115 | + %% Global |
| 116 | + subgraph GB_Sync ["Sync"] |
| 117 | + G1["**GlobalSignal**<br/>Global signal (sync)"] |
| 118 | + G2["**GlobalMemo**<br/>Derived global state (sync)"] |
| 119 | + G3["**Global<T>**<br/>A lazy value that is created once per application (sync)"] |
| 120 | + end |
| 121 | + |
| 122 | + E --> GB_Sync |
| 123 | + |
| 124 | + %% Route |
| 125 | + subgraph RT_Sync ["Sync"] |
| 126 | + R3["**Routable**<br/>The dioxus-router macro used for routing"] |
| 127 | + |
| 128 | + subgraph RT_Utils ["Utils"] |
| 129 | + R1["**use_route**<br/>Access information about the current routing location"] |
| 130 | + R2["**use_navigator**<br/>Access navigator to change router history"] |
| 131 | + R4["**use_outlet_context**<br/>Access to the outlet context for the route nesting level"] |
| 132 | + end |
| 133 | + end |
| 134 | + |
| 135 | + F --> RT_Sync |
| 136 | + |
| 137 | + %% Legend |
| 138 | + subgraph Legend ["Legend"] |
| 139 | + L_HIGHLIGHT["Frequently Used"] |
| 140 | + L_NORMAL["Standard"] |
| 141 | + end |
| 142 | + |
| 143 | + %% Styling |
| 144 | + classDef frequentlyUsed fill:#ff9,stroke:#333,stroke-width:2px |
| 145 | + class L_HIGHLIGHT,L1,L2,L3,L4,L5,L8,L14,C1,C2,R1,R2,R3,P1 frequentlyUsed --> |
0 commit comments