Building Your First Livewire 3.0 Component

Building Your First Livewire 3.0 Component

What is a Livewire Component?

A Livewire component is a self-contained unit of logic and UI. It contains both backend logic (written in PHP) and frontend rendering (done via Blade templates). With Livewire 3.0, these components are stored in the app/Livewiref older by default, ensuring a cleaner and more intuitive directory structure.

Step 1: Creating a Livewire Component

To create a new Livewire component, run the following command in your terminal:


php artisan make:livewire HelloWorld

This will create two files:

  1. app/Livewire/HelloWorld.php – The PHP class that contains the logic for your component.

  2. resources/views/livewire/hello-world.blade.php – The Blade template for the component’s UI.

Step 2: Understanding the Component Structure

HelloWorld.php

Here’s what the generated PHP class looks like:


<?php

namespace App\Livewire;

use Livewire\Component;

class HelloWorld extends Component
{
    public function render()
    {
        return view('livewire.hello-world');
    }
}

  • Namespace: Components are stored in the App\Livewire namespace.

  • render() method: This method defines the Blade view that will be rendered.

hello-world.blade.php

Here’s the generated Blade file:


<div>
    Hello, World!
</div>

This file will be displayed when the component is rendered.

Step 3: Using the Component

To use the component, include it in any Blade template, like welcome.blade.php:


<livewire:hello-world />

Visit your application in the browser (e.g., http://localhost:8000), and you’ll see:


Hello, World!

Step 4: Adding Dynamic Behavior

Let’s enhance the HelloWorld component to display dynamic content.

Update the PHP Class

Add a public property and a method to update it:


<?php

namespace App\Livewire;

use Livewire\Component;

class HelloWorld extends Component
{
    public $name = 'Livewire';

    public function updateName($newName)
    {
        $this->name = $newName;
    }

    public function render()
    {
        return view('livewire.hello-world');
    }
}

Update the Blade Template

Modify hello-world.blade.php to include a text input and a button:


<div>
    <h1>Hello, {{ $name }}!</h1>

    <input type="text" wire:model="name" placeholder="Enter your name">
    <button wire:click="updateName('Laravel')">Change Name to Laravel</button>
</div>

What’s Happening Here?

  1. wire:model: Binds the text input to the $name property in the PHP class. Changes to the input are reflected in real time.

  2. wire:click: Triggers the updateName() method when the button is clicked.

Step 5: Livewire Lifecycle Hooks

Livewire provides hooks to manage the lifecycle of your components. Here are some key hooks:

  1. mount(): Called when the component is initialized.

  2. updated($propertyName): Called after a property is updated.

  3. render(): Called to render the component’s Blade template.

Let’s add a mount() hook to set a default value for $name:


public function mount()
{
    $this->name = 'Livewire 3.0';
}

Final Output

With the above changes, visiting your page now allows users to:

  1. See Hello, Livewire 3.0! as the default text.

  2. Update the name dynamically by typing into the input field.

  3. Change the name to Laravel by clicking the button.

© Copyright 2025 Wontonee. All Right Reserved.

×How can I help you?