livewire / volt

Volt is an elegantly crafted functional API for Livewire.
https://livewire.laravel.com/docs/volt
MIT License
341 stars 19 forks source link

Using `mount` method from trait in Volt component #19

Closed jacksleight closed 1 year ago

jacksleight commented 1 year ago

Was advised to repost this from https://github.com/livewire/livewire/discussions/6158, not sure if it counts as a feature request or a bug:

I'm using a trait in a Volt component and I'd like to define a mount method in that trait that will be used by all components that implement it, rather than defining it for every component with the mount function. However at the moment the trait's mount method is not being called, even though I'm not using Volt's mount function.

This may also apply to other lifecycle methods, I've not tested those.

nunomaduro commented 1 year ago

At this time, it's complicated to have traits that override lifecycle hooks of Livewire when using the functional API. So, you have two options there:

  1. Use the regular class API: https://livewire.laravel.com/docs/volt#class-based-volt-components.
  2. Use the trait, but call it from your mount function. Something like this:
uses(YourTrait::class);

mount(fn () => $this->myMount());