Skip to content

Lifecycle hook methods

Magewire knows several lifecycle hook methods which can be optionally implemented by a components as needed.

Each hook method has a specific name. If a hook method exists on a component class, Magewire will automatically call it at the appropriate time.

Lifecycle hook method table

Keeping in mind the different phases during a component's lifecycle, we can choose which method to use for any given scenario.

lifecycle hook method called during preceding requests? called during subsequent requests? component hydrated? properties updated?
boot($blockData, $request)
mount($blockData, $request)
hydrate()
hydrateFoo($value)
booted()
updating($value, $prop)
updatingFoo($value)
updated($value, $prop)
updatedFoo($value)
dehydrate()
dehydrateFoo($value, $response)

In summary, during the preceding request, the available hook methods are:

  • boot($blockData, $request)
  • mount($blockData, $request)
  • booted()
  • dehydrate()
  • dehydrateFoo($value, $response)

During subsequent requests, the available hook methods are:

  • boot($blockData, $request)
  • hydrate()
  • hydrateFoo($value)
  • booted()
  • updating($value, $prop)
  • updatingFoo($value)
  • updated($value, $prop)
  • updatedFoo($value)
  • dehydrate()
  • dehydrateFoo($value, $response)

Note about Full Page Caching

For components on cached pages, no lifecycle hook methods will be called during the preceeding request, if the page is served from cache!

Only when a page is not in the cache, that is, when the component is rendered the first time, hook methods are called during the preceding request.

It is different for subsequent requests.
Subsequent requests are never served from the full-page cache.
They always bypass the full-page cache and are processed by Magento, because they are Ajax requests.
Thus all lifecycle hook methods are called during subsequent requests.

Property specific hook methods

Each property receiving a new value during a subsequent request will cause a matching updating*() method to be called, before the new value is set, and an updated*() method to be called after the new value is set.

For example, if a method name like updateFoo is used, Foo stands for the property name $foo.

The full list of property specific lifecycle hook methods:

public $foo;
public function hydrateFoo($value) {...}
public function updatingFoo($value) {...}
public function updatedFoo($value) {...}
public function dehydrateFoo($value, $response) {...}

This also works for camelCase properties:

public $fooBar;
public function updatingFooBar($value) {...}

And it even works with snake_case properties, even though are against any coding standard in the Magento context. The hook method names for snake_case properties use camelCase, too.

public $bar_baz;
public function updatingBarBaz($value) {...}

It is also possible to define callback methods targeting nested array properties.
Please refer to the page Working with Arrays for more information.

Lifecycle hook method arguments

There are several rules and conventions around hook method arguments:

  • If a hook method receives a $value argument, it must return it from the hook method, too.

    public function updatingFooBar($value)
    {
        // do custom logic
    
        return $value; // <-- important 
    }
    

  • The $blockData argument some hook methods receive is the array returned by $block->getData().

  • If a hook methods receives a $request argument, it will be an instance of \Magewirephp\Magewire\Model\RequestInterface.

  • When a hook method receives a $response argument, it will be an instance of \Magewirephp\Magewire\Model\ResponseInterface.