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

valentin_vranic's avatar

Livewire 3 wire:click and wire:target on $parent

Hi guys! I encountered an issue where if I call a $parent method on wire:click idk how to target it for the wire:loading to not trigger the parent's one but from the component itself. I have a general button downloadCSV without params. On child's view I'm calling it like this $parent.downloadCSV({{$call->call_id}}) with wire:target="downloadCSV({{$call->call_id}})" but still it doesn't trigger it. But the parent's button is the spinner (the functionality is okay, it works how it is imagined to) instead of the child's. How am I suppose to customize the wire:target of the child element to be triggered?

0 likes
4 replies
LaryAI's avatar
Level 58

To address the issue of targeting the correct element for wire:loading when calling a $parent method in Livewire 3, you need to ensure that the wire:target is correctly set to the method being called. However, since you're calling a parent method, you need to make sure that the child component's loading state is correctly managed.

Here's a step-by-step solution:

  1. Parent Component: Define the downloadCSV method in your parent component.
// ParentComponent.php
public function downloadCSV($callId)
{
    // Your logic to download CSV
}
  1. Child Component: In your child component, you will call the parent method using $parent.
// ChildComponent.php
public function triggerDownloadCSV($callId)
{
    $this->emitUp('downloadCSV', $callId);
}
  1. Child Blade View: In your child component's view, set up the button with wire:click and wire:loading directives.
<!-- child-component.blade.php -->
<button wire:click="triggerDownloadCSV({{ $call->call_id }})" wire:loading.attr="disabled">
    Download CSV
</button>
<div wire:loading wire:target="triggerDownloadCSV({{ $call->call_id }})">
    Loading...
</div>
  1. Parent Blade View: Ensure the parent component listens for the event emitted by the child component.
<!-- parent-component.blade.php -->
<div>
    @livewire('child-component')

    <script>
        Livewire.on('downloadCSV', callId => {
            @this.downloadCSV(callId);
        });
    </script>
</div>

Explanation:

  • Child Component: The triggerDownloadCSV method in the child component emits an event to the parent component using emitUp.
  • Child Blade View: The button in the child view uses wire:click to call the triggerDownloadCSV method. The wire:loading directive is used to show a loading state while the method is being processed.
  • Parent Blade View: The parent component listens for the downloadCSV event and calls the downloadCSV method with the provided callId.

This setup ensures that the loading state is managed within the child component, and the parent method is called correctly without interfering with the child's loading state.

By following these steps, you should be able to customize the wire:target of the child element to be triggered correctly.

Chingy's avatar
Chingy
Best Answer
Level 6

Well , Lary A.I. has been stuck at LW2 xD. Also it wouldn't do what you want, since it would only show a spinner for a split seconds of time, the time it needs to emit the event.

There is no way, as far as I know, that you can wire:target a method of parent component (and I believe it shouldn't as it would add too much complexity, imo).

What you could do though is a bit of "work-around" (from your perspective). wire:click in your nested Component, to a function that dispatches event to parent and after that bool property set to true to indicate that it is loading.

When the download CSV in parent component is done, dispatch to the child component that the loading is done, and set the bool property of loading to false.

1 like
Chingy's avatar

@valentin_vranic yeah but as far as i know, you can't do a wire:target the parent's component, so you should abandon the $parent.function call. Do it via events.

Please or to participate in this conversation.