What seems to be the problem:
So I have traits that I add to any component that needs it. I’m trying to hook into the available methods, like mount
and hydrate
, but it only works on some of the traits. The only method that works every time is initialize
. So if my trait was Foo
, then initializeFoo
would always run, but mountFoo
wouldn’t. Other trait called Bar
though will actually run mountBar
.
Steps to Reproduce:
Make like five (5) traits in your App\Traits
directory and use
them on a component. In each trait, add the respective initialize
and mount
method (append your trait name), and just log the method name so you can see if it gets called. Load up the component in the browser and confirm that every method has been called. Some load, some don’t.
Are you using the latest version of Livewire:
- Yes
Do you have any screenshots or code examples:
Here’s an example trait:
<?php
declare(strict_types = 1);
namespace App\Traits;
use Illuminate\Support\Facades\Log;
trait HasMultipleSteps
{
public int $step;
public function initializeHasMultipleSteps(): void
{
Log::info('initializeHasMultipleSteps');
}
public function mountHasMultipleSteps(): void
{
Log::info('mountHasMultipleSteps');
}
}
And the component is nothing special either:
<?php
declare(strict_types = 1);
namespace App\Http\Livewire;
use App\Traits\HasMultipleSteps;
use App\Traits\HasFileUploads;
use Livewire\Component;
final class Example extends Component
{
use HasFileUploads;
use HasMultipleSteps;
...
public function mount(): void
{
...
}
public function render()
{
return view('livewire.example');
}
}