Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Batman55's avatar
Level 32

Livewire not correctly rendering on refresh (Laravel 11)

This only happens when looping over and showing the Livewire component. Not if you just echo the body.

@foreach($comments as $comment)
	<livewire:idea-comment
			:key="$comment->id"
            :comment="$comment"
            :ideaUserId="$idea->user->id"
      />
@endforeach

When adding a new comment, refreshing only displays the new comment and the rest show as:

< wire:id="C6OA70MiuKBBsTshRlTF"> < wire:id="BrqLil0zVTRQi1eVqO94"> < wire:id="91WRXFeTzaC6eHX4TZJJ"> < wire:id="iirDWWA2x66S48s31gZD"> < wire:id="0GrEHCgm73x5LYdeWz1n"> < wire:id="b7VzzfOZg0EFFM8hSpVU"> < wire:id="kP9MpdUwFd5PDPw7x6OH"> < wire:id="lIjg2t7UNg9Z54l90dHc"> < wire:id="xArp2ktSPKYuPEMa69IW"> < wire:id="wVeFM6uSrH52E097r2rK"> 

In the livewire class:

protected $listeners = [
        'comment-was-added' => 'commentWasAdded'
    ];

    public function commentWasAdded(){
        $this->idea->refresh();
    }
0 likes
2 replies
LaryAI's avatar
Level 58

The issue you're experiencing with Livewire not rendering correctly on refresh might be related to how the component is handling the state and rendering the comments. Here are a few steps you can take to troubleshoot and potentially resolve the issue:

  1. Ensure Correct Data Binding: Make sure that your Livewire component is correctly binding the comments data. If you're using a property to store comments, ensure it's being updated properly when a new comment is added.

  2. Check the Blade Template: Verify that your Blade template is correctly iterating over the comments. For example:

    <div>
        @foreach($comments as $comment)
            <div wire:key="comment-{{ $comment->id }}">
                {{ $comment->content }}
            </div>
        @endforeach
    </div>
    

    Using wire:key helps Livewire efficiently update the DOM.

  3. Update the Component State: Ensure that the component's state is updated when a new comment is added. You might need to explicitly update the comments list in your commentWasAdded method:

    public function commentWasAdded()
    {
        $this->idea->refresh();
        $this->comments = $this->idea->comments; // Assuming $comments is a property
    }
    
  4. Check for Caching Issues: Sometimes, caching can cause issues with Livewire components. Clear your application cache and view cache:

    php artisan cache:clear
    php artisan view:clear
    
  5. Debugging: Add some debugging statements to ensure that the commentWasAdded method is being triggered and that the $this->idea object is being refreshed correctly.

  6. Livewire Version: Ensure you are using a compatible version of Livewire with Laravel 11. Check the Livewire documentation for any breaking changes or updates that might affect your implementation.

By following these steps, you should be able to identify and resolve the issue with Livewire not rendering correctly on refresh. If the problem persists, consider providing more context or code snippets for further assistance.

Batman55's avatar
Level 32

I did check out Larry's suggestions. Even retried them again (since I had done it other than the cache). If I take it out of a livewire component, everything works as it should. There are other Livewire components that are effected by the updating, and they also fail to rerender and just show wire=id"***"

Please or to participate in this conversation.