Get your next remote job on LaraJobs.
Laravel Livewire

Prevent a Livewire component from re-rendering

Benjamin Crozat
Published on Jan 10, 2024 0 comments Edit on GitHub
Prevent a Livewire component from re-rendering

Introduction to re-rendering prevention in Livewire

I previously talked about various way to re-render a Livewire component. But now, let’s do a 180° and talk about doing the opposite: preventing re-renders!

Block re-renders in a Livewire component

Sometimes, you might want to run an action or listen to an event in a Livewire component. Problem is: this triggers a re-render. The solution? The new Livewire\Attributes\Renderless attribute!

namespace App\Livewire;
 
use Livewire\Component;
use Livewire\Attributes\Renderless;
 
class Show extends Component
{
    #[Renderless] 
    public function incrementViewCount()
    {
        $this->model->incrementViewCount();
    }
}

This can be a huge win for the performances of your Laravel application.

Oh and by the way, if you still can’t deal with PHP’s new attributes, you can use the skipRender() method like so:

namespace App\Livewire;
 
use Livewire\Component;
 
class Show extends Component
{
    public function incrementViewCount()
    {
        $this->model->incrementViewCount();

        $thisskipRender();
    }
}

Wait, there's more!

Be the first to comment!

Get help or share something of value with other readers!

Great deals for enterprise developers
  • SaaSykit
    Launch your SaaS in days with a Laravel-based boilerplate.
    Get SaaSykit →
  • Summarize and talk to YouTube videos. Bypass ads, sponsors, chit-chat, and get to the point.
    Try Nobinge →
  • Monitor the health of your apps: downtimes, certificates, broken links, and more.
    20% off the first 3 months using the promo code CROZAT.
    Try Oh Dear for free
  • Keep the customers coming; monitor your Google rankings.
    30% off your first month using the promo code WELCOME30
    Try Wincher for free →
The latest community links
- / -