Troubleshooting
Dom Diffing Issues
The most common issues encountered by Livewire users has to do with Livewire's DOM diffing/patching system. This is the system that selectively updates elements that have been changed, added, or removed after every component update.
For the most part, this system is reliable, but there are certain cases where Livewire is unable to properly track changes. When this happens, hopefully, a helpful error will be thrown and you can debug with the following guide.
Symptoms
- An input element loses focus
- An element or group of elements dissapears suddenly
- A previously interactive element stops responding to user input
- A loading indicator mis-fires
- A user action no longer functions
Cures
- Ensure your component has a single-level root element
- Add
wire:key
to elements inside loops (the value towire:key
must be unique across the page):
1<ul>2 @foreach ($items as $item)3 <li wire:key="item-{{ $item->id }}">{{ $item }}</li>4 @endforeach5</ul>
- Add
key()
/wire:key
to nested components in a loop
1<ul>2 @foreach ($items as $item)3 @livewire('view-item', ['item' => $item], key('item-'.$item->id))4 5 <!-- key() using Laravel 7's tag syntax -->6 <livewire:view-item :item="$item" :wire:key="'item-'.$item->id">7 @endforeach8</ul>
- Wrap Blade conditionals (
@if
,@error
,@auth
) in an element
1<input type="text" wire:model="name">2<div> @error('name'){{ $message }}@enderror </div>
- Add
wire:key
. As a final measure, addingwire:key
will directly tell Livewire how to keep track of a DOM element. Over-using this attribute is a smell, but it is very useful and powerful for problems of this nature.
wire:key
must be entirely unique to that page. Meaning that you should prefix it, like wire:key="item-{{ $item->id }}"
, and avoid using $loop->index
to track the individual elements where you can.
1<div wire:key="foo">...</div>2<div wire:key="bar">...</div>
Checksum Issues
On every request, Livewire does a "checksum" but in some cases with arrays, it can throw an exception even when the data inside the array is the same.
Because in PHP an array can have keys that are alpha-numeric and numeric keys in the same array and in any order, but Javascript will make an object of it because it doesn't support arrays with keys that are alpha-numeric. When Javascript is creating an object it will also reorder the keys, it will place numeric keys before alpha-numeric keys.
This causes a problem when the JSON is sent back because the "checksum" will look different.
Some types (Point, LineString, Polygon, and the Multi- variations) will also fail this checksum.
So make sure when you have a public property that is an array numeric keys are before alpha-numeric character keys.
1class HelloWorld extends Component2{3 public $list = [4 '123' => 456,5 'foo' => 'bar'6 ];7 ...
Query String Issues
Livewire is using the site's referrer
information when setting the query string. This can lead to conflicts when you are adding security headers to your application through the referrer-policy
.
Symptoms
- The query string does not get updated at all.
- The query string does not get updated when the value is empty.
Cures
If you do set security headers, make sure the referrer-policy
value is set to same-origin
.
Root Element Issues
Livewire requires that there be only one HTML element at the root of a components blade view.
Having multiple root elements can mean that parts of your view won't work with Livewire correctly, if at all.
Symptoms
- A button isn't triggering a
wire:click
- Entering data into an input doesn't trigger a network request
- Parts of your view aren't updating properly (could also be a Dom Diffing issue, see above)
- You get an error in your browser console that says
Livewire: Multiple root elements detected. This is not supported.
- See below for an example of a component with a button that doesn't work:
1<div>2 Some content3</div>4 5<!-- This button isn't working -->6<button wire:click="doSomething">Do Something</button>
Cures
The solution is to ensure that you only have one root HTML element, such as a <div>
. If you have multiple elements, then wrap everything in a <div>
or another element that suits your layout.
So in our example from above, we have wrapped everything in a <div>
which gets the button running:
1<div> <!-- Added this wrapping div -->2 <div>3 Some content4 </div>5 6 <button wire:click="doSomething">Do Something</button>7</div> <!-- Added this closing tag for the wrapping div -->
Another cause can be using __construct() inside the Livewire class or a Trait.