How to force re-render a Livewire v3 component

How to force re-render a Livewire v3 component

Published
Jan 10, 2024
Written by
Benjamin Crozat
0
comments
2 minutes
read

Introduction to re-renderings in Livewire v3

Forcing components to re-render in Livewire is the secret for a better user experience. Keeping lists in sync by defering their management to the top component is the easiest way to do it. But sometimes, that’s just not enough and that where this article comes in handy.

Create an empty listener method

Let’s say that for some reasons, you have a child component that handles creating new resources and therefore, prevents the parent component from refreshing the list.

Well, I have a solution for you: create an empty listener method in your parent component.

Here’s the child component’s class:

namespace App\Livewire;

use Livewire\Component;

class Item extends Component
{
    public function create()
    {
        // Create the resource.

        $this->dispatch(‘created’);
    }
}

And here’s your parent component’s class, using the Livewire\Attributes\On attribute to let Livewire know it’s waiting for a given event:

namespace App\Livewire;

use Livewire\Component;
use Livewire\Attributes\On;

class Listing extends Component
{
    #[On(’created’)]
    public function refresh()
    {
    }
}

You can learn more about listeners in Livewire on the official documentation.

Use the secret $refresh method

Alternatively, you can listen for the “created” we made up for this article right in DOM. It’s a matter of preference, because both methods will work and produce the exact same result.

Here’s the parent component’s Blade view:

<div @created=“$refresh”>
    @foreach ($items as $item)
        <livewire:item :$item />
    @endforeach
</div>

You can also call it form Alpine.js using $wire.$render.

About Benjamin Crozat
Benjamin Crozat

Hi! I’m from the South of France and I’ve been a self-taught web developer since 2006. When I started learning PHP and JavaScript, PHP 4 was still widely used, Internet Explorer 6 ruled the world, and we used DHTML to add falling snow on websites.

Being able to educate myself for free on the web changed my life for the better. Giving back to the community was a natural direction in my career and I truly enjoy it.

Therefore, I decided to take action:

  1. I launched this blog in September 2022 with the goal to be in everyone’s Google search. I get more than tens of thousands of monthly clicks from it and even more visits overall (my analytics dashboard is public by the way).
  2. I also started growing my X (formerly Twitter) account at the same time, which has now over 7,000 followers.
  3. All the content I write is free thanks to my sponsors.

I also want to be completely free with my time and make a living with my own products. In April 2024, I launched Nobinge, a tool to summarize and chat with your content, including YouTube videos.

Believe me, I’m just getting started!

0 comments

You need to be signed in to comment this post.
Sign in with GitHub