Courses

Livewire 3 From Scratch: Practical Course

wire:loading Indicator

Summary of this lesson:
- Using wire:loading directive
- Adding loading indicators
- Managing loading opacity states
- Targeting specific loading actions

Imagine you have big data that needs to be loaded into a table. Besides the Lazy Loading component, another method uses wire:loading directive which has a bit more features.


First, add a div element with the wire:loading directive above the table.

resources/views/livewire/products.blade.php:

<div class="space-y-6">
<div class="space-x-8">
// ...
</div>
<div class="text-red-600" wire:loading>Loading...</div>
<div class="min-w-full align-middle">
// ...
</div>
{{ $products->links() }}
</div>

Simulate longer loading by adding sleep(1) to the Livewire component render method. Then try to search, and you should see a red text saying Loading....

wire loading

The wire:loading directive also accepts adding or removing class parameters. For example, we can add opacity to the table while loading.

resources/views/livewire/products.blade.php:

<div class="space-y-6">
// ...
<div class="text-red-600" wire:loading>Loading...</div>
<div class="min-w-full align-middle" wire:loading.class="opacity-50"> // [tl! highlight]
// ...
</div>
{{ $products->links() }}
</div>

wire loading opacity

You can target a specific action by adding the wire:target="methodName" directive.

<form wire:submit="save">
<!-- ... -->
 
<button type="submit">Save</button>
 
<button type="button" wire:click="remove">Remove</button>
 
<div wire:loading wire:target="remove"> // [tl! highlight]
Removing post...
</div>
</form>

You can read more about Loading States in the official documentation.

Previous: Live-Search in the Table
avatar

why, when I write something, the first time the download works and gives out posts, the next process does not work just loading and opacity-50

avatar

Can you expand on this? Not sure I fully understand what you had in mind

avatar
You can use Markdown
avatar
Francisco Ferreira Roque Júnior

Hey Polivas, The "You can target a specific action by adding the wire:target="methodName" directive." looks like don't belongs to this class, but if it does, sorry, I just don't understand the context.

Thanks sir.

avatar

In this example we have a button with an action remove. Where we use wire:loading we alsp use the wire:target and specify that remove method. This means while remove is being executed the wire:loading div will be active. Hope this helps.

avatar
Francisco Ferreira Roque Júnior

Thank you very much Nerijus, now I understood the point.

avatar
You can use Markdown
avatar

I search for the lesson of how to make the loading indicator at Livewire , alpine js and Js I see this lesson at laraveldaily but now i can't get useful result from the search

avatar

If you need help will need more than that

avatar
You can use Markdown
avatar
You can use Markdown