Livewire Development
When to Apply
Activate this skill when:
- Creating or modifying Livewire components
- Using wire: directives (model, click, loading, sort, intersect)
- Implementing islands or async actions
- Writing Livewire component tests
Documentation
Use search-docs for detailed Livewire 4 patterns and documentation.
Basic Usage
Creating Components
Single-file component (default in v4)
{{ $assist->artisanCommand('make:livewire create-post') }}
Multi-file component
{{ $assist->artisanCommand('make:livewire create-post --mfc') }}
Class-based component (v3 style)
{{ $assist->artisanCommand('make:livewire create-post --class') }}
With namespace
{{ $assist->artisanCommand('make:livewire Posts/CreatePost') }}
Converting Between Formats
Use php artisan livewire:convert create-post to convert between single-file, multi-file, and class-based formats.
Component Format Reference
| Format |
Flag |
Structure |
| Single-file (SFC) |
default |
PHP + Blade in one file |
| Multi-file (MFC) |
--mfc |
Separate PHP class, Blade, JS, tests |
| Class-based |
--class |
Traditional v3 style class |
| View-based |
⚡ prefix |
Blade-only with functional state |
Single-File Component Example
Livewire 4 Specifics
Key Changes From Livewire 3
These things changed in Livewire 4, but may not have been updated in this application. Verify this application's setup to ensure you follow existing conventions.
- Use
Route::livewire() for full-page components; config keys renamed: layout → component_layout, lazy_placeholder → component_placeholder.
wire:model now ignores child events by default (use wire:model.deep for old behavior); wire:scroll renamed to wire:navigate:scroll.
- Component tags must be properly closed;
wire:transition now uses View Transitions API (modifiers removed).
- JavaScript:
$wire.$js('name', fn) → $wire.$js.name = fn; commit/request hooks → interceptMessage()/interceptRequest().
New Features
- Component formats: single-file (SFC), multi-file (MFC), view-based components.
- Islands (
@island) for isolated updates; async actions (wire:click.async, #[Async]) for parallel execution.
- Deferred/bundled loading:
defer, lazy.bundle for optimized component loading.
| Feature |
Usage |
Purpose |
| Islands |
@island(name: 'stats') |
Isolated update regions |
| Async |
wire:click.async or #[Async] |
Non-blocking actions |
| Deferred |
defer attribute |
Load after page render |
| Bundled |
lazy.bundle |
Load multiple together |
New Directives
wire:sort, wire:intersect, wire:ref, .renderless, .preserve-scroll are available for use.
data-loading attribute automatically added to elements triggering network requests.
| Directive |
Purpose |
wire:sort |
Drag-and-drop sorting |
wire:intersect |
Viewport intersection detection |
wire:ref |
Element references for JS |
.renderless |
Component without rendering |
.preserve-scroll |
Preserve scroll position |
Best Practices
- Always use
wire:key in loops
- Use
wire:loading for loading states
- Use
wire:model.live for instant updates (default is debounced)
- Validate and authorize in actions (treat like HTTP requests)
Configuration
smart_wire_keys defaults to true; new configs: component_locations, component_namespaces, make_command, csp_safe.
Alpine & JavaScript
wire:transition uses browser View Transitions API; $errors and $intercept magic properties available.
- Non-blocking
wire:poll and parallel wire:model.live updates improve performance.
For interceptors and hooks, see reference/javascript-hooks.md.
Testing
Livewire::test(Counter::class)
->assertSet('count', 0)
->call('increment')
->assertSet('count', 1);
Verification
- Browser console: Check for JS errors
- Network tab: Verify Livewire requests return 200
- Ensure
wire:key on all @foreach loops
Common Pitfalls
- Missing
wire:key in loops → unexpected re-rendering
- Expecting
wire:model real-time → use wire:model.live
- Unclosed component tags → syntax errors in v4
- Using deprecated config keys or JS hooks
- Including Alpine.js separately (already bundled in Livewire 4)
1---
2name: livewire-development
3description: >-
4 Develops reactive Livewire 4 components. Activates when creating, updating, or modifying
5 Livewire components; working with wire:model, wire:click, wire:loading, or any wire: directives;
6 adding real-time updates, loading states, or reactivity; debugging component behavior;
7 writing Livewire tests; or when the user mentions Livewire, component, counter, or reactive UI.
8metadata:
9 source: https://skillmd.com/skills/spatie/livewire-development
10---
11
12# Livewire Development
13
14## When to Apply
15
16Activate this skill when:
17
18- Creating or modifying Livewire components
19- Using wire: directives (model, click, loading, sort, intersect)
20- Implementing islands or async actions
21- Writing Livewire component tests
22
23## Documentation
24
25Use `search-docs` for detailed Livewire 4 patterns and documentation.
26
27## Basic Usage
28
29### Creating Components
30
31<code-snippet name="Component Creation Commands" lang="bash">
32
33# Single-file component (default in v4)
34
35{{ $assist->artisanCommand('make:livewire create-post') }}
36
37# Multi-file component
38
39{{ $assist->artisanCommand('make:livewire create-post --mfc') }}
40
41# Class-based component (v3 style)
42
43{{ $assist->artisanCommand('make:livewire create-post --class') }}
44
45# With namespace
46
47{{ $assist->artisanCommand('make:livewire Posts/CreatePost') }}
48
49</code-snippet>
50
51### Converting Between Formats
52
53Use `php artisan livewire:convert create-post` to convert between single-file, multi-file, and class-based formats.
54
55### Component Format Reference
56
57| Format | Flag | Structure |
58|--------|------|-----------|
59| Single-file (SFC) | default | PHP + Blade in one file |
60| Multi-file (MFC) | `--mfc` | Separate PHP class, Blade, JS, tests |
61| Class-based | `--class` | Traditional v3 style class |
62| View-based | ⚡ prefix | Blade-only with functional state |
63
64### Single-File Component Example
65
66<code-snippet name="Single-File Component Example" lang="php">
67
68<?php
69use Livewire\Component;
70
71new class extends Component {
72 public int $count = 0;
73
74 public function increment(): void
75 {
76 $this->count++;
77 }
78}
79?>
80
81<div>
82 <button wire:click="increment">Count: @{{ $count }}</button>
83</div>
84
85</code-snippet>
86
87## Livewire 4 Specifics
88
89### Key Changes From Livewire 3
90
91These things changed in Livewire 4, but may not have been updated in this application. Verify this application's setup to ensure you follow existing conventions.
92
93- Use `Route::livewire()` for full-page components; config keys renamed: `layout` → `component_layout`, `lazy_placeholder` → `component_placeholder`.
94- `wire:model` now ignores child events by default (use `wire:model.deep` for old behavior); `wire:scroll` renamed to `wire:navigate:scroll`.
95- Component tags must be properly closed; `wire:transition` now uses View Transitions API (modifiers removed).
96- JavaScript: `$wire.$js('name', fn)` → `$wire.$js.name = fn`; `commit`/`request` hooks → `interceptMessage()`/`interceptRequest()`.
97
98### New Features
99
100- Component formats: single-file (SFC), multi-file (MFC), view-based components.
101- Islands (`@island`) for isolated updates; async actions (`wire:click.async`, `#[Async]`) for parallel execution.
102- Deferred/bundled loading: `defer`, `lazy.bundle` for optimized component loading.
103
104| Feature | Usage | Purpose |
105|---------|-------|---------|
106| Islands | `@island(name: 'stats')` | Isolated update regions |
107| Async | `wire:click.async` or `#[Async]` | Non-blocking actions |
108| Deferred | `defer` attribute | Load after page render |
109| Bundled | `lazy.bundle` | Load multiple together |
110
111### New Directives
112
113- `wire:sort`, `wire:intersect`, `wire:ref`, `.renderless`, `.preserve-scroll` are available for use.
114- `data-loading` attribute automatically added to elements triggering network requests.
115
116| Directive | Purpose |
117|-----------|---------|
118| `wire:sort` | Drag-and-drop sorting |
119| `wire:intersect` | Viewport intersection detection |
120| `wire:ref` | Element references for JS |
121| `.renderless` | Component without rendering |
122| `.preserve-scroll` | Preserve scroll position |
123
124## Best Practices
125
126- Always use `wire:key` in loops
127- Use `wire:loading` for loading states
128- Use `wire:model.live` for instant updates (default is debounced)
129- Validate and authorize in actions (treat like HTTP requests)
130
131## Configuration
132
133- `smart_wire_keys` defaults to `true`; new configs: `component_locations`, `component_namespaces`, `make_command`, `csp_safe`.
134
135## Alpine & JavaScript
136
137- `wire:transition` uses browser View Transitions API; `$errors` and `$intercept` magic properties available.
138- Non-blocking `wire:poll` and parallel `wire:model.live` updates improve performance.
139
140For interceptors and hooks, see [reference/javascript-hooks.md](reference/javascript-hooks.md).
141
142## Testing
143
144<code-snippet name="Testing Example" lang="php">
145
146Livewire::test(Counter::class)
147 ->assertSet('count', 0)
148 ->call('increment')
149 ->assertSet('count', 1);
150
151</code-snippet>
152
153## Verification
154
1551. Browser console: Check for JS errors
1562. Network tab: Verify Livewire requests return 200
1573. Ensure `wire:key` on all `@foreach` loops
158
159## Common Pitfalls
160
161- Missing `wire:key` in loops → unexpected re-rendering
162- Expecting `wire:model` real-time → use `wire:model.live`
163- Unclosed component tags → syntax errors in v4
164- Using deprecated config keys or JS hooks
165- Including Alpine.js separately (already bundled in Livewire 4)