Skip to main content

Module extract

Module extract 

Source
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

ExtractorGives 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
Parentthe data loaded by the layouts above
Requestthe 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

ExtractorGives you
&mut D / &Dthe data the load returned (() without one), checked at compile time
&RouteErrorthe error, in error pages
State<&T>the app state, or any part of it that implements FromRef, borrowed
Navthe router: links, goto, back/forward, loading, invalidation, shallow routing, blocking
Scrollthe page’s scroll area, whose position the router restores
PageState<T>state the view owns while its page is shown
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
Navigationhow the page was reached: from where, how, in which history direction
Requestthe url the page was loaded with, and its route’s parameters
Remote<S>queries and commands: data and mutations outside of loads

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

LivesMutable by views
State<&T>as long as the app, shared by everythingthrough Cell / RefCell
&mut D (the load’s data)until the load rerunsyes
PageState<T>while the page (or layout) is shown, across invalidationsyes
Snapshot<T>like PageState, and restored when coming back with back/forwardyes
HistoryState<&T>with its history entry, set by Nav::push_statethrough Nav
PersistentState<T>across app restarts, shared by the whole appyes

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.

Re-exports§

pub use crate::remote::Remote;

Structs§

HistoryState
The state attached to the current history entry, if it is a T.
Nav
The router, as seen by a view: navigating, loading, shallow routing and blocking.
PageState
State a view owns for as long as its page is shown.
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.
PersistentState
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.
State
The app state passed to Router::with_state, or a part of it that implements FromRef.

Traits§

FromRef
A part of the app state that State can extract on its own, e.g. State<&Db> for an AppState with a db field.
FromRequest
Types that can be extracted from a navigation, used as arguments of hooks and loads.
FromView
Types that can be extracted for a view, used as arguments of page, layout and error views.