Defer Loading

Livewire offers a wire:init directive to run an action as soon as the component is rendered. This can be helpful in cases where you don't want to hold up the entire page load, but want to load some data immediately after the page load.

use Livewire\Component;

class ShowPosts extends Component
{
    protected $posts = [];

    public function loadPosts()
    {
        $this->posts = Post::all();
    }

    public function render()
    {
        return view('livewire.show-posts', [
            'posts' => $this->posts,
        ]);
    }
}
<div wire:init="loadPosts">
    <ul>
        @foreach ($posts as $post)
            <li>{{ $post->title }}</li>
        @endforeach
    </ul>
</div>

The loadPosts action will be run imediately after the Livewire component renders on the page.

← Previous Topic

Nesting Components

Next Topic →

Prefetching