|
| 1 | +--- |
| 2 | +name: livewire-development |
| 3 | +description: "Use for any task or question involving Livewire. Activate if user mentions Livewire, wire: directives, or Livewire-specific concepts like wire:model, wire:click, wire:sort, or islands, invoke this skill. Covers building new components, debugging reactivity issues, real-time form validation, drag-and-drop, loading states, migrating from Livewire 3 to 4, converting component formats (SFC/MFC/class-based), and performance optimization. Do not use for non-Livewire reactive UI (React, Vue, Alpine-only, Inertia.js) or standard Laravel forms without Livewire." |
| 4 | +license: MIT |
| 5 | +metadata: |
| 6 | + author: laravel |
| 7 | +--- |
| 8 | + |
| 9 | +# Livewire Development |
| 10 | + |
| 11 | +## Documentation |
| 12 | + |
| 13 | +Use `search-docs` for detailed Livewire 4 patterns and documentation. |
| 14 | + |
| 15 | +## Basic Usage |
| 16 | + |
| 17 | +### Creating Components |
| 18 | + |
| 19 | +```bash |
| 20 | + |
| 21 | +# Single-file component (default in v4) |
| 22 | + |
| 23 | +php artisan make:livewire create-post |
| 24 | + |
| 25 | +# Multi-file component |
| 26 | + |
| 27 | +php artisan make:livewire create-post --mfc |
| 28 | + |
| 29 | +# Class-based component (v3 style) |
| 30 | + |
| 31 | +php artisan make:livewire create-post --class |
| 32 | + |
| 33 | +# With namespace |
| 34 | + |
| 35 | +php artisan make:livewire Posts/CreatePost |
| 36 | +``` |
| 37 | + |
| 38 | +### Converting Between Formats |
| 39 | + |
| 40 | +Use `php artisan livewire:convert create-post` to convert between single-file, multi-file, and class-based formats. |
| 41 | + |
| 42 | +### Choosing a Component Format |
| 43 | + |
| 44 | +Before creating a component, check `config/livewire.php` for directory overrides, which change where files are stored. Then, look at existing files in those directories (defaulting to `app/Livewire/` and `resources/views/livewire/`) to match the established convention. |
| 45 | + |
| 46 | +### Component Format Reference |
| 47 | + |
| 48 | +| Format | Flag | Class Path | View Path | |
| 49 | +|--------|------|------------|-----------| |
| 50 | +| Single-file (SFC) | default | — | `resources/views/livewire/create-post.blade.php` (PHP + Blade in one file) | |
| 51 | +| Multi-file (MFC) | `--mfc` | `app/Livewire/CreatePost.php` | `resources/views/livewire/create-post.blade.php` | |
| 52 | +| Class-based | `--class` | `app/Livewire/CreatePost.php` | `resources/views/livewire/create-post.blade.php` | |
| 53 | +| View-based | ⚡ prefix | — | `resources/views/livewire/create-post.blade.php` (Blade-only with functional state) | |
| 54 | + |
| 55 | +Namespaced components map to subdirectories: `make:livewire Posts/CreatePost` creates files at `app/Livewire/Posts/CreatePost.php` and `resources/views/livewire/posts/create-post.blade.php`. |
| 56 | + |
| 57 | +### Single-File Component Example |
| 58 | + |
| 59 | +<!-- Single-File Component Example --> |
| 60 | +```php |
| 61 | +<?php |
| 62 | +use Livewire\Component; |
| 63 | + |
| 64 | +new class extends Component { |
| 65 | + public int $count = 0; |
| 66 | + |
| 67 | + public function increment(): void |
| 68 | + { |
| 69 | + $this->count++; |
| 70 | + } |
| 71 | +} |
| 72 | +?> |
| 73 | + |
| 74 | +<div> |
| 75 | + <button wire:click="increment">Count: @{{ $count }}</button> |
| 76 | +</div> |
| 77 | +``` |
| 78 | + |
| 79 | +## Livewire 4 Specifics |
| 80 | + |
| 81 | +### Key Changes From Livewire 3 |
| 82 | + |
| 83 | +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. |
| 84 | + |
| 85 | +- Use `Route::livewire()` for full-page components (e.g., `Route::livewire('/posts/create', CreatePost::class)`); config keys renamed: `layout` → `component_layout`, `lazy_placeholder` → `component_placeholder`. |
| 86 | +- `wire:model` now ignores child events by default (use `wire:model.deep` for old behavior); `wire:scroll` renamed to `wire:navigate:scroll`. |
| 87 | +- Component tags must be properly closed; `wire:transition` now uses View Transitions API (modifiers removed). |
| 88 | +- JavaScript: `$wire.$js('name', fn)` → `$wire.$js.name = fn`; `commit`/`request` hooks → `interceptMessage()`/`interceptRequest()`. |
| 89 | + |
| 90 | +### New Features |
| 91 | + |
| 92 | +- Component formats: single-file (SFC), multi-file (MFC), view-based components. |
| 93 | +- Islands (`@island`) for isolated updates; async actions (`wire:click.async`, `#[Async]`) for parallel execution. |
| 94 | +- Deferred/bundled loading: `defer`, `lazy.bundle` for optimized component loading. |
| 95 | + |
| 96 | +| Feature | Usage | Purpose | |
| 97 | +|---------|-------|---------| |
| 98 | +| Islands | `@island(name: 'stats')` | Isolated update regions | |
| 99 | +| Async | `wire:click.async` or `#[Async]` | Non-blocking actions | |
| 100 | +| Deferred | `defer` attribute | Load after page render | |
| 101 | +| Bundled | `lazy.bundle` | Load multiple together | |
| 102 | + |
| 103 | +### New Directives |
| 104 | + |
| 105 | +- `wire:sort`, `wire:intersect`, `wire:ref`, `.renderless`, `.preserve-scroll` are available for use. |
| 106 | +- `data-loading` attribute automatically added to elements triggering network requests. |
| 107 | + |
| 108 | +| Directive | Purpose | |
| 109 | +|-----------|---------| |
| 110 | +| `wire:sort` | Drag-and-drop sorting | |
| 111 | +| `wire:intersect` | Viewport intersection detection | |
| 112 | +| `wire:ref` | Element references for JS | |
| 113 | +| `.renderless` | Component without rendering | |
| 114 | +| `.preserve-scroll` | Preserve scroll position | |
| 115 | + |
| 116 | +## Best Practices |
| 117 | + |
| 118 | +- Always use `wire:key` in loops |
| 119 | +- Use `wire:loading` for loading states |
| 120 | +- Use `wire:model.live` for instant updates (default is debounced) |
| 121 | +- Validate and authorize in actions (treat like HTTP requests) |
| 122 | + |
| 123 | +## Configuration |
| 124 | + |
| 125 | +- `smart_wire_keys` defaults to `true`; new configs: `component_locations`, `component_namespaces`, `make_command`, `csp_safe`. |
| 126 | + |
| 127 | +## Alpine & JavaScript |
| 128 | + |
| 129 | +- `wire:transition` uses browser View Transitions API; `$errors` and `$intercept` magic properties available. |
| 130 | +- Non-blocking `wire:poll` and parallel `wire:model.live` updates improve performance. |
| 131 | + |
| 132 | +For interceptors and hooks, see [reference/javascript-hooks.md](reference/javascript-hooks.md). |
| 133 | + |
| 134 | +## Testing |
| 135 | + |
| 136 | +<!-- Testing Example --> |
| 137 | +```php |
| 138 | +Livewire::test(Counter::class) |
| 139 | + ->assertSet('count', 0) |
| 140 | + ->call('increment') |
| 141 | + ->assertSet('count', 1); |
| 142 | +``` |
| 143 | + |
| 144 | +## Verification |
| 145 | + |
| 146 | +1. Browser console: Check for JS errors |
| 147 | +2. Network tab: Verify Livewire requests return 200 |
| 148 | +3. Ensure `wire:key` on all `@foreach` loops |
| 149 | + |
| 150 | +## Common Pitfalls |
| 151 | + |
| 152 | +- Missing `wire:key` in loops → unexpected re-rendering |
| 153 | +- Expecting `wire:model` real-time → use `wire:model.live` |
| 154 | +- Unclosed component tags → syntax errors in v4 |
| 155 | +- Using deprecated config keys or JS hooks |
| 156 | +- Including Alpine.js separately (already bundled in Livewire 4) |
0 commit comments