pub struct ComponentContext {
pub id: String,
pub name: String,
pub mounting: bool,
pub request_metadata: Map<String, Value>,
pub effects: Map<String, Value>,
pub memo: Map<String, Value>,
pub request: Option<Arc<Request>>,
pub streamed: Vec<Value>,
}Expand description
The context one lifecycle hook or action receives.
Fields§
§id: StringThe component’s instance ID.
name: StringThe component’s registered name, such as counter.
mounting: boolWhether this render is the component’s first.
request_metadata: Map<String, Value>Metadata the browser sent with the call, such as island or async.
effects: Map<String, Value>Effects the component adds during this request.
memo: Map<String, Value>Memo entries the component adds during this request.
request: Option<Arc<Request>>The browser’s request, so an action can read the signed-in user, run a policy, or use a service without the framework cloning the request per component.
streamed: Vec<Value>The frames an action streamed, in the order it wrote them.
Implementations§
Source§impl ComponentContext
impl ComponentContext
Sourcepub fn mounting(
id: impl Into<String>,
name: impl Into<String>,
request: Option<Arc<Request>>,
) -> Self
pub fn mounting( id: impl Into<String>, name: impl Into<String>, request: Option<Arc<Request>>, ) -> Self
A context for a component that is mounting, in a request that has request.
Sourcepub fn updating(
id: impl Into<String>,
name: impl Into<String>,
request: Option<Arc<Request>>,
) -> Self
pub fn updating( id: impl Into<String>, name: impl Into<String>, request: Option<Arc<Request>>, ) -> Self
A context for a component that is updating.
Sourcepub async fn validate(&mut self, component: &dyn Component) -> Result<()>
pub async fn validate(&mut self, component: &dyn Component) -> Result<()>
Runs the component’s declared rules against its current state.
Every failing field’s first message is recorded in the snapshot’s memo.errors, which the
browser’s $errors reads, and the whole set comes back in LivewireError::ValidationFailed
so the caller can answer without an HTTP failure.
§Errors
Returns LivewireError::ValidationFailed when a rule rejects a field, and the framework’s
own error when a rule could not run at all, such as a check that needs the database.
Sourcepub fn add_error(
&mut self,
field: impl Into<String>,
message: impl Into<String>,
)
pub fn add_error( &mut self, field: impl Into<String>, message: impl Into<String>, )
Records a validation error for field, which the browser reads from the snapshot’s
memo.errors and shows through $errors.
Sourcepub fn stream(&mut self, name: impl Into<String>, content: impl Into<String>)
pub fn stream(&mut self, name: impl Into<String>, content: impl Into<String>)
Sends content to the element marked wire:stream="name", as the action runs.
The browser appends it as the frame arrives, so a long action can show progress; the ordinary response follows the frames.
Sourcepub fn add_effect(&mut self, name: &str, value: Value)
pub fn add_effect(&mut self, name: &str, value: Value)
Adds value under name, replacing any value already there.
Sourcepub fn xjs(&mut self, expression: impl Into<String>)
pub fn xjs(&mut self, expression: impl Into<String>)
Queues a JavaScript expression the browser evaluates once the answer morphed.
The client reads effects.xjs as a list of {expression, params} entries, so a second
call appends instead of replacing what the first one asked for.
Sourcepub fn push_effect(&mut self, name: &str, value: Value)
pub fn push_effect(&mut self, name: &str, value: Value)
Adds value to the list under name, keeping a list that is already there.
Sourcepub fn download(
&mut self,
name: impl Into<String>,
content: impl AsRef<[u8]>,
content_type: impl Into<String>,
)
pub fn download( &mut self, name: impl Into<String>, content: impl AsRef<[u8]>, content_type: impl Into<String>, )
Sends content to the browser as a file, instead of replacing the component’s markup.
The response carries the file base64-encoded in the download effect, which the runtime
turns into a save dialog; the component renders nothing.