SKILL.blade.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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, invoke this skill. Covers building new components, debugging reactivity issues, real-time form validation, loading states, migrating from Livewire 2 to 3, 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. @php
  9. /** @var \Laravel\Boost\Install\GuidelineAssist $assist */
  10. @endphp
  11. # Livewire Development
  12. ## Documentation
  13. Use `search-docs` for detailed Livewire 3 patterns and documentation.
  14. ## Basic Usage
  15. ### Creating Components
  16. Use the `{{ $assist->artisanCommand('make:livewire [Posts\\CreatePost]') }}` Artisan command to create new components.
  17. ### Fundamental Concepts
  18. - State should live on the server, with the UI reflecting it.
  19. - All Livewire requests hit the Laravel backend; they're like regular HTTP requests. Always validate form data and run authorization checks in Livewire actions.
  20. ## Livewire 3 Specifics
  21. ### Key Changes From Livewire 2
  22. These things changed in Livewire 3, but may not have been updated in this application. Verify this application's setup to ensure you follow existing conventions.
  23. - Use `wire:model.live` for real-time updates, `wire:model` is now deferred by default.
  24. - Components now use the `App\Livewire` namespace (not `App\Http\Livewire`).
  25. - Use `$this->dispatch()` to dispatch events (not `emit` or `dispatchBrowserEvent`).
  26. - Use the `components.layouts.app` view as the typical layout path (not `layouts.app`).
  27. ### New Directives
  28. - `wire:show`, `wire:transition`, `wire:cloak`, `wire:offline`, `wire:target` are available for use.
  29. ### Alpine Integration
  30. - Alpine is now included with Livewire; don't manually include Alpine.js.
  31. - Plugins included with Alpine: persist, intersect, collapse, and focus.
  32. ## Best Practices
  33. ### Component Structure
  34. - Livewire components require a single root element.
  35. - Use `wire:loading` and `wire:dirty` for delightful loading states.
  36. ### Using Keys in Loops
  37. @boostsnippet("Wire Key in Loops", "blade")
  38. @foreach ($items as $item)
  39. <div wire:key="item-{{ $item->id }}">
  40. {{ $item->name }}
  41. </div>
  42. @endforeach
  43. @endboostsnippet
  44. ### Lifecycle Hooks
  45. Prefer lifecycle hooks like `mount()`, `updatedFoo()` for initialization and reactive side effects:
  46. @boostsnippet("Lifecycle Hook Examples", "php")
  47. public function mount(User $user) { $this->user = $user; }
  48. public function updatedSearch() { $this->resetPage(); }
  49. @endboostsnippet
  50. ## JavaScript Hooks
  51. You can listen for `livewire:init` to hook into Livewire initialization:
  52. @boostsnippet("Livewire Init Hook Example", "js")
  53. document.addEventListener('livewire:init', function () {
  54. Livewire.hook('request', ({ fail }) => {
  55. if (fail && fail.status === 419) {
  56. alert('Your session expired');
  57. }
  58. });
  59. Livewire.hook('message.failed', (message, component) => {
  60. console.error(message);
  61. });
  62. });
  63. @endboostsnippet
  64. ## Testing
  65. @boostsnippet("Example Livewire Component Test", "php")
  66. Livewire::test(Counter::class)
  67. ->assertSet('count', 0)
  68. ->call('increment')
  69. ->assertSet('count', 1)
  70. ->assertSee(1)
  71. ->assertStatus(200);
  72. @endboostsnippet
  73. @boostsnippet("Testing Livewire Component Exists on Page", "php")
  74. $this->get('/posts/create')
  75. ->assertSeeLivewire(CreatePost::class);
  76. @endboostsnippet
  77. ## Common Pitfalls
  78. - Forgetting `wire:key` in loops causes unexpected behavior when items change
  79. - Using `wire:model` expecting real-time updates (use `wire:model.live` instead in v3)
  80. - Not validating/authorizing in Livewire actions (treat them like HTTP requests)
  81. - Including Alpine.js separately when it's already bundled with Livewire 3