|
1 | | - |
2 | | -#### Key Changes from Livewire 2 |
3 | | - |
4 | | -- **Namespace**: Components now use `App\Livewire` (not `App\Http\Livewire`) |
5 | | -- **Events**: Use `$this->dispatch()` (not `emit` or `dispatchBrowserEvent`) |
6 | | -- **Layout path**: `components.layouts.app` (not `layouts.app`) |
7 | | -- **Deferred by default**: Use `wire:model.live` for real-time updates |
8 | | -- **Alpine included**: Don't manually include Alpine.js |
9 | | - |
10 | | -#### Livewire Best Practices |
11 | | - |
12 | | -- **Single root element** in Blade components |
13 | | -- **Add wire:key** in loops: |
14 | | - |
| 1 | +## Key Changes from Livewire 2 |
| 2 | +- These changed in Livewire 2, but may not have been updated in this project. Verify this project's setup to ensure you conform with project conventions. |
| 3 | +- **Wire:model**: Use `wire:model.live` for real-time updates, `wire:model` is now deferred by default. |
| 4 | +- **Namespace**: Components now use `App\Livewire` (not `App\Http\Livewire`). |
| 5 | +- **Events**: Use `$this->dispatch()` (not `emit` or `dispatchBrowserEvent`). |
| 6 | +- **Layout path**: `components.layouts.app` (not `layouts.app`). |
| 7 | + |
| 8 | +## New directives |
| 9 | +- `wire:show`, `wire:transition`, `wire:cloak, `wire:offline`, `wire:target` are available for use. Use the docs to find usages. |
| 10 | + |
| 11 | +## Alpine |
| 12 | +- Alpine is now included with Livewire, don't manually include Alpine.js. |
| 13 | +- Plugins built in to Alpine: persist, intersect, collapse, and focus. |
| 14 | + |
| 15 | +## Lifecycle hooks |
| 16 | +- You can listen for `livewire:init` to hook into Livewire initialization, and `fail.status === 419` for the page expiring: |
15 | 17 | @verbatim |
16 | | -```blade |
17 | | -@foreach ($items as $item) |
18 | | - <div wire:key="item-{{ $item->id }}"> |
19 | | - {{ $item->name }} |
20 | | - </div> |
21 | | -@endforeach |
22 | | -``` |
| 18 | +<code-snippet name="livewire:load example" lang="js"> |
| 19 | +document.addEventListener('livewire:init', function () { |
| 20 | + Livewire.hook('request', ({ fail }) => { |
| 21 | + if (fail && fail.status === 419) { |
| 22 | + alert('Your session expired'); |
| 23 | + } |
| 24 | + }); |
| 25 | + |
| 26 | + Livewire.hook('message.failed', (message, component) => { |
| 27 | + console.error(message); |
| 28 | + }); |
| 29 | +}); |
| 30 | +</code-snippet> |
23 | 31 | @endverbatim |
24 | | - |
25 | | -- **Use attributes** for event listeners: |
26 | | - |
27 | | -```php |
28 | | -#[On('todo-created')] |
29 | | -public function refreshList() |
30 | | -{ |
31 | | -// ... |
32 | | -} |
33 | | -``` |
34 | | - |
35 | | -- **Loading states**: Use `wire:loading` and `wire:dirty` |
36 | | -- **Confirmations**: Use `wire:confirm="Are you sure?"` |
0 commit comments