You're browsing the documentation for an old version of Livewire. Consider upgrading your project to Livewire 2.x.

Loading States

Because Livewire makes a roundtrip to the server every time an action is triggered on the page, there are cases when the page may not react immediately to a user event (like a click). Livewire allows you to easily display loading states, which can make your app feel more responsive.

Toggling elements during "loading" states

Elements with the wire:loading directive are only visible while waiting for actions to complete (network requests).

<div>
    <button wire:click="checkout">Checkout</button>

    <div wire:loading>
        Processing Payment...
    </div>
</div>

When the "Checkout" button is clicked, the "Processing Payment..." message will show. When the action is finished, the message will disappear.

If you want to avoid flickering because loading is very fast, you can add the .delay modifier, and it will only show up if loading takes longer than 200ms.

You can also "hide" an element during a loading state using the .remove modifier.

<div>
    <button wire:click="checkout">Checkout</button>

    <div wire:loading.remove>
        Hide Me While Loading...
    </div>
</div>

Targeting specific actions

The method outlined above works well for simple components. For more complex components, you may want to show loading indicators only for specific actions.

<div>
    <button wire:click="checkout">Checkout</button>
    <button wire:click="cancel">Cancel</button>

    <div wire:loading wire:target="checkout">
        Processing Payment...
    </div>
</div>

In the above example, the loading indicator will be displayed when the "Checkout" button is clicked, but not when the "Cancel" button is clicked.

wire:target can accept multiple arguments in a comma separated format like this: wire:target="foo, bar".

In addition to actions, you can also target whenever a wire:model is synchronized.

<div>
    <input wire:model="quantity">

    <div wire:loading wire:target="quantity">
        Updating quantity...
    </div>
</div>

Toggling classes

You can add or remove classes from an element during loading states, by adding the .class modifier to the wire:loading directive.

<div>
    <button wire:click="checkout" wire:loading.class="bg-gray">
        Checkout
    </button>
</div>

Now, when the "Checkout" button is clicked, the background will turn gray while the network request is processing.

You can also perform the inverse and remove classes by adding the .remove modifier.

<div>
    <button wire:click="checkout" wire:loading.class.remove="bg-blue" class="bg-blue">
        Checkout
    </button>
</div>

Now the bg-blue class will be removed from the button while loading.

Toggling attributes

Similar to classes, HTML attributes can be added or removed from elements during loading states:

<div>
    <button wire:click="checkout" wire:loading.attr="disabled">
        Checkout
    </button>
</div>

Now, when the "Checkout" button is clicked, the disabled="true" attribute will be added to the element while loading.

← Previous Topic

Nesting Components

Next Topic →

Polling