Skip to main content

LivewireState

Trait LivewireState 

Source
pub trait LivewireState:
    Send
    + Sync
    + 'static {
    // Required methods
    fn wire_fields() -> &'static [WireField]
       where Self: Sized;
    fn dehydrate_fields(&self, data: &mut Map<String, Value>);
    fn hydrate_fields(&mut self, data: &Map<String, Value>) -> Result<()>;
    fn set_field(&mut self, field: &str, value: Value) -> Result<()>;
    fn default_view(&self) -> ComponentView;

    // Provided methods
    fn component_name() -> &'static str
       where Self: Sized { ... }
    fn render_values(&self, data: &mut Map<String, Value>) { ... }
    fn validation_rules(&self) -> Rules { ... }
}
Expand description

The wire-public state of a component, generated by #[derive(LivewireComponent)].

The derive writes one arm per field in declaration order, so the server never reflects over a type at runtime and skipped fields never touch the wire.

Required Methods§

Source

fn wire_fields() -> &'static [WireField]
where Self: Sized,

The wire-public fields, in declaration order.

Source

fn dehydrate_fields(&self, data: &mut Map<String, Value>)

Writes the wire-public fields into data, in declaration order.

Source

fn hydrate_fields(&mut self, data: &Map<String, Value>) -> Result<()>

Reads the fields present in data. A field that is absent keeps its default.

§Errors

Fails when a present value cannot be hydrated into its field.

Source

fn set_field(&mut self, field: &str, value: Value) -> Result<()>

Sets one wire-public field, without interpreting dots in its name.

§Errors

Fails when the field is not wire-public, or when the value cannot be assigned.

Source

fn default_view(&self) -> ComponentView

The view the component renders by default.

Provided Methods§

Source

fn component_name() -> &'static str
where Self: Sized,

The component’s name, which the registry replaces with the name the application registered and which diagnostics use until then.

Source

fn render_values(&self, data: &mut Map<String, Value>)

The values a template reads, which are the fields as they cross the wire unless the component shows more.

The derive overrides this for #[livewire(model)] fields: the view reads the fetched row, while the snapshot keeps only the reference.

Source

fn validation_rules(&self) -> Rules

The rules the component’s fields declare, ready to run against its own state.

The derive generates this from #[livewire(validate(..))]; a component without those returns an empty set.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§