Courses

Livewire 3 From Scratch: Practical Course

Hooks: updated() and updatedField()

Summary of this lesson:
- Using updated lifecycle hook
- Implementing dynamic property updates
- Working with dot notation properties

Livewire has a variety of lifecycle hooks that allows you to execute code at a specific time. In this lesson, let's see the updated hook.


The updated hook can be called in two ways every time the property is updated. The first way is to have an updated method that accepts two parameters, $property and $value.

Let's update the post title into a headline. Every word will have a capital first letter. In the Livewire component, we add an updated method.

use Illuminate\Support\Str;
 
class CreatePost extends Component
{
public PostForm $form;
 
// ...
 
public function updated($property): void
{
if ($property == 'form.title') {
$this->form->title = Str::headline($this->form->title);
}
}
 
// ...
}

Because we added all of the post properties to the Form Object, we can check if the updated property is correct using the dot notation. If it is the form.title, we set the title to the headline using the Laravel helper.

Now after the title is written, the title gets changed.

updated lifecycle hook

But there is a second method where...

The full lesson is only for Premium Members.
Want to access all 30 lessons of this course? (108 min read)

You also get:

  • 73 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord