Back End Programming

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:

    
     
    
   
  • 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:

    
     
Hello, World!

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:

    
     
    
   

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:

    
     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:

    
     

Hello, {{ $name }}!

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.

teamwontonee

Share
Published by
teamwontonee

Recent Posts

Introduction to Laravel Livewire

What is Laravel Livewire?Laravel Livewire is a full-stack framework for Laravel that simplifies the development…

2 days ago

Laravel And Vue Contact Form

In this tutorial we will learn about how to create a contact form in vue…

3 years ago

Basics Of Laravel And VUE JS

Laravel is a popular open-source framework developed by Taylor otwell for PHP. Laravel comes with…

3 years ago

Google Verified SMS

SMS play a vital role in our day-to-day life. For instance, if we want to…

3 years ago

Bagisto Laravel Razorpay Extension

Bagisto is an open-source Laravel eCommerce application. Bagisto is one of the popular eCommerce applications…

3 years ago

Bagisto Laravel Paytm Extension

Bagisto is an open-source Laravel eCommerce application. Bagisto is one of the popular eCommerce applications…

3 years ago

This website uses cookies.