The render() method

A Livewire component class needs only one method to function properly: render(). This method fires on every component update and is in charge of returning the Blade view to be rendered.

Returning Blade

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 views have only ONE root HTML 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>
Although `render()` methods closely resemble controller methods, there are a few techniques you are used to using in controllers that aren't available in Livewire components. Here are two common things you might forget ARE NOT possible in Livewire:
public function render()
{
    return back();
    // Or
    return ['some' => 'data'];
}
← Previous Topic

Making Components

Next Topic →

State Management