The render()
method
A Livewire component's render
method gets called on the initial page load AND every subsequent component update.
In simple components, you don't need to define a `render` method yourself. The base Livewire component class has a dynamic `render` method included.
Returning Blade Views
The render()
method is expected to return a Blade view, therefore, you can compare it to writing a controller method. Here is an example:
Make sure your Blade view only has ONE root element.
use Livewire\Component;
class ShowPosts extends Component
{
public function render()
{
return view('livewire.show-posts', [
'posts' => Post::all(),
]);
}
}
<div>
@foreach ($posts as $post)
@include('includes.post', $post)
@endforeach
</div>
Returning Blade Template Strings
If your Livewire project uses Laravel 7 or above, you can optionally return a Blade template string from ->render()
.
use Livewire\Component;
class DeletePost extends Component
{
public $post;
public function mount(Post $post)
{
$this->post = $post;
}
public function delete()
{
$this->post->delete();
}
public function render()
{
return <<<'blade'
<div>
<button wire:click="delete">Delete Post</button>
</div>
blade;
}
}
For inline components like above, you should use the
--inline
flag during creation: artisan make:livewire delete-post --inline
← Previous Topic
Making Components
Next Topic →
The