forked from livewire/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
defer-loading.blade.php
40 lines (35 loc) · 977 Bytes
/
defer-loading.blade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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.
@component('components.code-component')
@slot('class')
@verbatim
class ShowPost extends Component
{
public $readyToLoad = false;
public function loadPosts()
{
$this->readyToLoad = true;
}
public function render()
{
return view('livewire.show-posts', [
'posts' => $this->readyToLoad
? Post::all()
: [],
]);
}
}
@endverbatim
@endslot
@slot('view')
@verbatim
<div wire:init="loadPosts">
<ul>
@foreach ($posts as $post)
<li>{{ $post->title }}</li>
@endforeach
</ul>
</div>
@endverbatim
@endslot
@endcomponent
The `loadPosts` action will be run immediately after the Livewire component renders on the page.