Lifecycle hooks in Livewire allow you to execute logic at specific stages of a component’s lifecycle. They give you precise control over how and when certain parts of your component are rendered or updated. Livewire 3.0 introduces new hooks that make components more powerful and flexible.
Here are some commonly used lifecycle hooks in Livewire 3.0:
mount()
Called when the component is initialized.
public function mount($id = null)
{
$this->product = Product::find($id);
}
render()
Called to render the component’s Blade template.
public function render()
{
return view('livewire.product-detail');
}
updated($propertyName)
Triggered after a property is updated.
public function updated($propertyName)
{
$this->validateOnly($propertyName);
}
commit.prepare
Triggered before changes are committed to the DOM.
public function commitPrepare()
{
// Pre-process data before committing to the DOM
}
morph.updated
Runs after the DOM has been updated with changes.
public function morphUpdated()
{
// Handle logic after the DOM is updated
}
hydrate()
Executed when the component is hydrated on a subsequent request.
public function hydrate()
{
// Perform actions like loading fresh data
}
Let’s create a Product Detail Component that uses advanced lifecycle hooks for real-time updates and seamless DOM interaction.
Run the command to create the component:
php artisan make:livewire ProductDetail
product = Product::find($id);
}
public function hydrate()
{
// Ensure product data is always fresh
$this->product = Product::find($this->product->id);
}
public function render()
{
return view('livewire.product-detail', [
'product' => $this->product,
]);
}
public function commitPrepare()
{
// Logic before DOM update
}
public function morphUpdated()
{
// Logic after DOM update
}
}
Livewire 3.0 introduces SPA-like navigation for building seamless, single-page applications without a front-end framework. You can now navigate between components without a full-page reload.
In your web.php
routes file, define the routes:
use App\Http\Livewire\ProductDetail;
Route::get('/product/{id}', ProductDetail::class)->name('product.detail');
Create a simple product list with clickable links:
@foreach ($products as $product)
-
{{ $product->name }}
@endforeach
wire:navigate
The wire:navigate
directive enables SPA-like navigation between components without refreshing the page.
Here’s how lifecycle hooks and SPA navigation work together:
mount()
: Initializes the product data when navigating to a product detail page.
hydrate()
: Ensures data is refreshed when navigating back.
commit.prepare
and morph.updated
: Provide fine control over DOM updates during navigation.
With the above setup, your application will:
Load product details dynamically without full-page reloads.
Provide seamless SPA-like navigation between pages.
Use advanced lifecycle hooks for real-time updates and data freshness.
410 C, Jaina Tower-1, District Center, Janak Puri New Delhi-110058, India.
© Copyright 2025 Wontonee. All Right Reserved.