Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Svennnn's avatar

Flux/Livewire render accordeon-content conditionally?

Hey,

I have a livewire component whichs updateCalls take roughly 300ms. Only 30ms of those are actual business-logic, IO and stuff, the rest comes from many many nested fluxcomponents that are diffed. At least thats what I am guessing from the profiler-image.

My component is using a flux:accordion exclusive, where within each of the 3 content-sections, is a nested foreach which prints a lot of flux:badge.

My idea is now to test wether or not I can improve performance by skipping a foreach, if the content isn't visible anyway. I am using an exclusive accordeon anyway.

Is there something within flux that enables me to listen to the currently opened accordeon-item?

0 likes
1 reply
LaryAI's avatar
Level 58

To improve the performance of your Livewire component by conditionally rendering content based on the visibility of accordion sections, you can leverage Livewire's ability to listen to events and conditionally render content.

Here's a general approach you can take:

  1. Emit an Event on Accordion Toggle: When an accordion section is toggled, emit an event to notify Livewire about the change in visibility.

  2. Listen for the Event in Livewire: In your Livewire component, listen for the event and update a property that tracks which accordion section is currently open.

  3. Conditionally Render Content: Use the property to conditionally render the content of the accordion sections.

Here's a basic example to illustrate this approach:

JavaScript (Frontend)

First, ensure you have a way to emit an event when an accordion section is toggled. You might use a JavaScript function to handle this:

document.addEventListener('DOMContentLoaded', function () {
    document.querySelectorAll('.accordion-toggle').forEach(button => {
        button.addEventListener('click', function () {
            const sectionId = this.getAttribute('data-section-id');
            Livewire.emit('accordionToggled', sectionId);
        });
    });
});

Livewire Component

In your Livewire component, listen for the accordionToggled event and update a property:

use Livewire\Component;

class AccordionComponent extends Component
{
    public $openSection = null;

    protected $listeners = ['accordionToggled'];

    public function accordionToggled($sectionId)
    {
        $this->openSection = $sectionId;
    }

    public function render()
    {
        return view('livewire.accordion-component', [
            // Pass data to the view as needed
        ]);
    }
}

Blade View

In your Blade view, conditionally render the content based on the openSection property:

<div>
    <div class="accordion">
        <button class="accordion-toggle" data-section-id="section1">Section 1</button>
        @if($openSection === 'section1')
            <div class="accordion-content">
                @foreach($items as $item)
                    <flux:badge>{{ $item }}</flux:badge>
                @endforeach
            </div>
        @endif

        <button class="accordion-toggle" data-section-id="section2">Section 2</button>
        @if($openSection === 'section2')
            <div class="accordion-content">
                @foreach($otherItems as $item)
                    <flux:badge>{{ $item }}</flux:badge>
                @endforeach
            </div>
        @endif

        <!-- Repeat for other sections -->
    </div>
</div>

Explanation

  • JavaScript: Listens for clicks on accordion toggle buttons and emits a Livewire event with the section ID.
  • Livewire Component: Listens for the accordionToggled event and updates the $openSection property.
  • Blade View: Conditionally renders the content of each accordion section based on the $openSection property.

This approach should help reduce unnecessary rendering and improve the performance of your component by only rendering the content of the currently open accordion section.

Please or to participate in this conversation.