livewire·rust

Composition

A component renders other components, passes them HTML, props, and events, and the server keeps each child addressable across updates.

nested-wrapper.livewire
<div class="card">
    <h2>Posts</h2>
    <livewire:post-row :post="post" wire:key="post-{{ post.id }}" />
</div>
Live exampleopen ↗

Bump the parent and watch the child re-render; the parent passes the prop, the child skips while it is unchanged.

A parent's template renders its children as it renders itself. The server mounts each child, records it in the parent's memo.children, and injects the child's own wire:id, wire:snapshot, and wire:effects attributes. A later update of the parent does not mount those children again: it emits a stub with the child's ID, so the browser morphs the child in place and the child keeps its state.

Nesting

Inside a .livewire view, write the tag. In a plain .html page, call the function the tag compiles to.

<div class="card">
    <h2>Posts</h2>
    <livewire:post-row :post="post" wire:key="post-{{ post.id }}" />
</div>
{{ livewire("counter", {"count": 1}, "counter-1") }}

A bound attribute (:post="post") becomes a named mount parameter. A plain attribute becomes a prop when the child declares a field of that name, and is otherwise split off as a forwarded HTML attribute. wire:key names the child in the parent's record; wire:ref, lazy, defer, and any @-prefixed parameter are reserved by the protocol and never become props.

The parent's snapshot holds one entry per child, keyed by the parent's key for it, with the child's root tag and its instance ID:

"memo": {
    "id": "kOa7wQ2cJv9tLbX1mR4d",
    "name": "posts",
    "children": { "post-1": ["div", "n3Pq8sVtY2bKcW7hJ5Lm"] },
    "release": "a-a-a"
}

On the next render of the parent, each recorded child becomes its stub, and the browser then updates the child with its own request:

<div wire:id="n3Pq8sVtY2bKcW7hJ5Lm" wire:name="post-row" wire:key="post-1"></div>

A key you write always wins. With smart_wire_keys enabled (the default), a nested component without a key that sits inside a loop receives one derived from the template's identity, its position in the source, and each enclosing loop.index0. Set smart_wire_keys = false in the livewire config section to turn that off; the compiler then passes no key.

Slots

The content of a paired tag is passed to the child as its slot. It is rendered by the parent, never stored in the data object, and reaches the child's view as safe markup.

<livewire:nested-child :step="step">
    <em>from the parent</em>
</livewire:nested-child>
<div class="card">
    <p>Step: {{ step }}</p>
    <div data-slot>{{ slot }}</div>
</div>

The server keeps the slot in memo.slots so every later render of the child still has it. When a parent re-renders a child it stubs, the slot does not disappear: the server wraps the content in fragment markers and sends it as an effects.slotFragments entry, which the browser morphs inside the child's element.

A fragment is a comment pair, and the browser reads its metadata as key=value pairs separated by |:

<!--[if FRAGMENT:type=slot|name=slot|token=slot|id=n3Pq8sVtY2bKcW7hJ5Lm]><![endif]-->
    <em>from the parent</em>
<!--[if ENDFRAGMENT:type=slot|name=slot|token=slot|id=n3Pq8sVtY2bKcW7hJ5Lm]><![endif]-->

This port has no x-slot syntax, so only the default slot can be filled from a template.

Forwarded attributes are split off, not merged. The server separates a plain attribute from the mount parameters and records it in the request metadata, but it does not add it to the child's root element. Write the attribute in the child's own markup instead.

Reactive props and modelable children

A field marked #[livewire(reactive)] takes its value from the parent: whatever the parent renders the child with, under that name, is the child's value. The server stores the parameters the parent passed, lists the reactive field names in the child's memo.props, and applies the parent's value before the child renders.

#[derive(Default, LivewireComponent)]
#[livewire(view = "livewire.nested-child")]
pub struct NestedChild {
    #[livewire(reactive)]
    pub step: i64,
}

An update can then be answered without a render. When the child's props are unchanged from what the parent passed in the same batch, the update carries no property change, and every call is $commit, the response for that child is only:

{"skip": true, "id": "n3Pq8sVtY2bKcW7hJ5Lm"}

Modelable bindings are not implemented. #[livewire(modelable)] is accepted and recorded, but nothing reads it: a child cannot bind its own field to a parent's wire:model. There is no memo.bindings and no bindingsDirectives in a snapshot this port writes.

Lazy and deferred components

Mark the component's #[component] attribute. The first render is a placeholder: one empty root element carrying wire:init="__lazyLoad" and a memo flag that says the component has not loaded.

#[derive(Default, LivewireComponent)]
#[livewire(view = "livewire.deferred")]
pub struct Deferred {
    pub title: String,
}

#[component(lazy)]
impl Deferred {}

On mount the server writes "lazyLoaded": false into the snapshot and renders <div></div>. The browser fires __lazyLoad, which is a protocol signal rather than a method: the action allowlist accepts it and returns nothing. The update that follows renders the real view, and the snapshot it returns carries "lazyLoaded": true. defer takes the same path in this port: both options produce the same placeholder and the same wire:init signal.

The placeholder is the only lazy path. This port does not force it for state that needs an await. A model reference is fetched in the update pipeline, before the update's actions and render, and a page route fetches its data before it mounts the component. Pass already-fetched rows to a component as props, or hold a ModelRef and let the update resolve it.

A lazy child that receives a slot renders its placeholder without that slot content, which appears with the real view. Upstream's placeholder slot is not implemented.

Islands

An island is a region of a component that can be re-rendered alone. Write @island around it; the compiler turns the block into the fragment pair that the browser's island code parses.

@island(name: 'log')
    <p data-count>{{ count }}</p>
@endisland
<!--[if FRAGMENT:type=island|name=log|token=1f2e3d4c-0|mode=morph]><![endif]-->
    <p data-count>0</p>
<!--[if ENDFRAGMENT:type=island|name=log|token=1f2e3d4c-0|mode=morph]><![endif]-->

The options are name, mode (morph, append, or prepend), and the lazy pair lazy: true or defer: true. A lazy island gets wire:intersect.once="__lazyLoadIsland" on its first element; a deferred one gets wire:init="__lazyLoadIsland". A block with no name uses its token, and two islands always have distinct tokens.

When an action runs inside an island, the browser sends the island's name and mode in the call's metadata. The server then renders the component and answers with that island's fragment in effects.islandFragments instead of the whole markup in effects.html; the fragment's markers are re-stamped with the mode the browser asked for, which is how wire:island.append and wire:island.prepend reach it. If the fragment cannot be found, the response falls back to the full markup.

__lazyLoadIsland is answered like any other update: a full render of the component. The island's own markup is what the client then places.

Teleport and persist

Both directives compile to Alpine attributes; the browser runtime does the work.

@teleport('#modal')
    <div class="dialog">Saved.</div>
@endteleport

@persist('player')
    <div id="player">...</div>
@endpersist

The first becomes <template x-teleport="#modal">...</template>, the second <div x-persist="player">...</div>, because Alpine requires a wrapper element there. Neither is a server feature, so neither survives if Alpine is not on the page.

Scripts and assets

A component's @script and @assets blocks render once per component type and template position in a request, not once per instance. The snapshot records which blocks ran, so the browser does not run them again on a later update.

@script
    <script>
        window.__counterScripts = (window.__counterScripts || 0) + 1;
    </script>
@endscript

Two counters on one page emit that block once, and their snapshots carry "scripts": ["counter:script:0"]. An @assets block is recorded the same way in memo.assets, with its own counter.

The update response also carries an assets list next to components. It is filled from the assets a request reports through LivewireRequest::add_asset, and nothing in this port reports any yet, so it is empty.

Single-file components and modules

A component names its view with #[livewire(view = "livewire.counter")], which loads resources/views/livewire/counter.livewire, or carries the markup itself:

#[derive(Default, LivewireComponent)]
#[livewire(inline = "<div>{{ count }}</div>")]
pub struct Counter {
    pub count: i32,
}

A JavaScript or CSS file beside the component is served by the module routes, at a path built from the component name. The name is encoded because . and : are legal in a component name and not in a path segment: . becomes --, :: becomes ---, and : becomes ----. So pages::counter is served at /livewire-1a2b3c4d/js/pages---counter.js. The files are read from view_path and component_path, a name that would leave those directories answers 404, and so does a component with no such file.

No module effects. The server never sends scriptModule, childScriptModules, styleModule, or globalStyleModule, so a module is fetched only when something on the page asks for its URL. Scoped CSS is not injected automatically.

Move a component between the two forms with the console command:

cargo run -- livewire:convert counter

Converting a single-file component writes the sibling .livewire view and rewrites the attribute to #[livewire(view = "livewire.counter")]. Converting the other way inlines the view and leaves the file behind; add --remove-view to delete it. The command rewrites the module it reads, so it needs no --force.

What is not here

FeatureState
Modelable bindingsAttribute accepted, not read; no memo.bindings
Named slots, x-slotAbsent; only the default slot
Placeholder slot for a lazy childAbsent; the slot arrives with the loaded view
Component JS/CSS effectsRoutes serve the files; no effect asks for them
Forwarded HTML attributesSeparated from props, never merged into the child's root element
File, form, wireable, and paginator synthsAbsent; model references are covered in Models & persistence