Expand description
Extractors: the arguments of hooks, loads and views.
Hooks and loads are async functions whose arguments implement FromRequest; views are
functions of a &mut egui::Ui whose other arguments implement FromView. The extractors
can be in any order.
§In hooks and loads
| Extractor | Gives you |
|---|---|
Path<T> | the route’s path parameters, deserialized with serde |
Query<T> | the query string, deserialized with serde |
State<T> | a clone of the app state, or of any part of it that implements FromRef |
Parent | the data loaded by the layouts above |
Request | the url and the route’s parameters, untyped |
What a load extracts decides when it reruns: Path and Query make it depend on the
parameters and the query, Parent on the layouts above.
§In views
| Extractor | Gives you |
|---|---|
&mut D / &D | the data the load returned (() without one), checked at compile time |
&RouteError | the error, in error pages |
LayoutData<T> | the data of the nearest layout around the view that loaded a T |
State<&T> | the app state, or any part of it that implements FromRef, borrowed |
Nav | the router: links, goto, back/forward, loading, invalidation, shallow routing, blocking |
Scroll | the page’s scroll area, whose position the router restores |
PageState<T> | state the view owns while its page is shown |
PageStateFrom<T> | like PageState, created from the load’s data |
Snapshot<T> | like PageState, and restored when coming back with back/forward |
PersistentState<T> | app-wide state that survives app restarts, like localStorage |
HistoryState<&T> | the shallow-routing state of the current history entry |
Navigation | how the page was reached: from where, how, in which history direction |
Request | the url the page was loaded with, and its route’s parameters |
Layout views also get the Outlet as their second argument; it is not an
extractor, every layout has exactly one.
§Borrowing and mutating
Views run synchronously within the frame, so what they extract is borrowed from the router:
nothing big is cloned every frame. Hooks and loads are futures that can outlive the frame,
so they get owned values, cloned once per navigation.
§Which state to use
| Lives | Mutable by views | |
|---|---|---|
State<&T> | as long as the app, shared by everything | through Cell / RefCell |
&mut D (the load’s data) | until the load reruns | yes |
PageState<T> | while the page (or layout) is shown, across invalidations | yes |
Snapshot<T> | like PageState, and restored when coming back with back/forward | yes |
HistoryState<&T> | with its history entry, set by Nav::push_state | through Nav |
PersistentState<T> | across app restarts, shared by the whole app | yes |
A rule of thumb: what you loaded goes in the load’s data, what the user did goes in a
PageState or a Snapshot, and preferences go in a PersistentState. The app state
is shared by every view at once, so no view can get &mut access to it.
Structs§
- History
State - The state attached to the current history entry, if it is a
T. - Layout
Data - The data loaded by the nearest layout around the view that loaded a
T: whatParentis toloads, for views. - Nav
- The router, as seen by a view: navigating, loading, shallow routing and blocking.
- Page
State - State a view owns for as long as its page is shown.
- Page
State From - Like
PageState, but created from the view’s data (what itsloadreturned) withT::from(&data), the first time it is extracted. - Parent
- Access to the data loaded by the layouts above, in a layout’s or page’s
load. - Path
- Typed path parameters, deserialized with serde.
- Persistent
State - State that outlives the app, saved with egui’s persisted memory.
- Query
- Typed query parameters, deserialized with
serde_urlencoded. Fails with a 400. - Scroll
- The page’s scroll area, an extractor: new pages start at the top (unless navigated to with
GotoOptions::no_scroll), and back/forward restores where the user was. - Snapshot
- Like
PageState, but kept when the user navigates away and restored when they come back with back/forward. - Snapshot
From - Like
Snapshot, but created from the view’s data (what itsloadreturned) withT::from(&data), like aPageStateFrom. A snapshot that comes back with back/forward keeps its value. - State
- The app state passed to
Router::with_state, or a part of it that implementsFromRef.
Traits§
- FromRef
- A part of the app state that
Statecan extract on its own, e.g.State<&Db>for anAppStatewith adbfield. - From
Request - Types that can be extracted from a navigation, used as arguments of hooks and
loads. - From
View - Types that can be extracted for a view, used as arguments of page, layout and error views.