Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/Livewire

Lifecycle Hooks in Livewire v4

Understand component lifecycle methods.

1 February 20261 min readLivewireاقرأ بالعربية

On this page

  1. Complete Lifecycle Example

Livewire v4 components have lifecycle hooks that run at specific times.

Complete Lifecycle Example

php
class UserProfile extends Component
{
    public User $user;
    public string $name;
    public string $email;
    
    // Runs once when component is first created
    public function mount(User $user): void
    {
        $this->user = $user;
        $this->name = $user->name;
        $this->email = $user->email;
    }
    
    // Runs on every request (before hydration)
    public function boot(): void
    {
        // Good for authorization checks
        if (!auth()->user()->can('view', $this->user)) {
            abort(403);
        }
    }
    
    // Runs after component is hydrated from state
    public function hydrate(): void
    {
        // Re-initialize non-persisted data
    }
    
    // Runs before a specific property updates
    public function updatingName($value): void
    {
        $this->name = trim($value);
    }
    
    // Runs after a specific property updates  
    public function updatedName($value): void
    {
        $this->validate(['name' => 'required|min:2']);
    }
    
    // Runs before any property updates
    public function updating($property, $value): void
    {
        logger("Updating {$property} to {$value}");
    }
    
    // Runs after any property updates
    public function updated($property, $value): void
    {
        logger("{$property} updated to {$value}");
    }
    
    public function render(): View
    {
        return view('livewire.user-profile');
    }
}
All articles

Keep reading

  • Authorization in Livewire v4 Components1 February 20261 min read
  • Blaze Pre-Compiler: Massive Performance Gains in Livewire v41 February 20261 min read
  • Building a Bookmark Feature with Livewire v41 February 20261 min read