sajadsholi's avatar

render a livewire compoenent

I have a helper that renders a Livewire component like a Laravel component. Does anyone know if rendering a Livewire component with the Blade facade can produce unwanted bugs or problems in Livewire?


function changeEnumStateComponent(int|float $id, array $states, array $state)
{
    $key = 'change_enum_state_' . $id;
    return Blade::render(
        '<livewire:admin.c.change-enum-state :id="$id" :states="$states" :state="$state" :key="$key" />',
        compact('id', 'states', 'state', 'key')
    );
}

0 likes
1 reply
Jsanwo64's avatar
Level 11

Yes, using Blade::render() to render Livewire components can cause subtle bugs with interactivity, component lifecycle, and DOM diffing. It’s fine for static HTML, but if you want fully working Livewire components, you should use Livewire::mount() (or @livewire() in Blade).

If your goal is “render a Livewire component dynamically in PHP helpers”, use Livewire’s Logic:

so that would mean that you have something like this

use Livewire\Livewire;

function changeEnumStateComponent(int|string $id, array $states, array $state)
{
    return Livewire::mount('admin.c.change-enum-state', [
        'id' => $id,
        'states' => $states,
        'state' => $state,
        'key' => 'change_enum_state_' . $id,
    ])->html();
}

Try it out and see what happens

1 like

Please or to participate in this conversation.