7

This is the Livewire framework for Laravel

HTML:

<button wire:click="$emit('postAdded')">

PHP:

protected $listeners = ['postAdded' => 'showPostAddedMessage'];

public function showPostAddedMessage()
{
    // Do stuff
}

public function render()
{
    return view('livewire.index');
}

Clicking the button calls showPostAddedMessage() and after that render(). How can I listen for an event without calling render()?

6
  • 1
    This is how livewire works. whenever you are changing anything / firing any event. the component will refresh. As far as I learned livewire, there is no way to stop it unless you are putting die() isnide showPOstAddedMessage function, which will be a very wierd way to solve it. Commented Aug 12, 2020 at 5:14
  • @fahim152 Oh, this makes sense. I have to seperate the part that loads the message and the part that sends it. Thank you very much! Could you post your comment as an answer, too so I can mark it as solved? :) Commented Aug 12, 2020 at 13:28
  • 1
    I solved my problem by nesting 2 components. The parent fires an event, the child component does not update and this is exactly what I was looking for! Commented Aug 12, 2020 at 13:39
  • 1
    congrats .... I didn't think of it that way. Great solution btw... My comment is not a solution tbh. its just a comment. it doesn't solved what you are looking for brother. It would be great if you write down your solution in the answer section so that others can get help in the future :) Commented Aug 13, 2020 at 7:04
  • @fahim152 Firing an event is the reason a component is being refreshed, your words and the answer to my question (not my problem!!!) so please add this as an answer :)) Commented Aug 13, 2020 at 15:57

4 Answers 4

7

I had the same issue. In my case, the render() method was closing my parent modal box. I just added "wire:ignore" in modal div. Livewire ignores that div whenever it calls the render() method

Sign up to request clarification or add additional context in comments.

2 Comments

Each time I select any item from drop down render method called and my particular div where suppose to play video get disappear, so I put wire:ignore on that div and it works.
Thanks, that was exactly what I was looking for :)
5

I've came to this sort of hack: if your concern is to not re-render the view, you can just return an empty string from render(). The DOM will not be updated.
My case: I've got a download method that is not supposed to render anything.

public function download()
{
   $this->skipRender();
}

public function render()
{
    if($this->shouldSkipRender) {
        return '';
    }
    
    return view("xxxxx");
}

As long as I'm not missing anything, this works for me.

1 Comment

Thanks, you can also add "wire:ignore" to the first div in the livewire component :)
3

This is how livewire works. whenever you are changing anything / firing any event. the component will refresh. As far as I learned livewire, there is no way to stop it unless you are putting die() isnide showPOstAddedMessage function, which will be a very wierd way to solve it.

Comments

1

For anyone coming across this problem now, there is new functionality in Livewire 3 that deals with this.

Livewire 3 Component has the skipRender() that can be called after carrying out the logic of your event handler.

#[On('some_event')]
public function handleSomeEvent()
{
    -- event code here

    $this->skipRender();
}

Incidentally, in the documentation, there is also a #[Renderless] attribute that can be added to methods to indicate that they should not cause the component to be re-rendered. However, I found that this had no effect on a method that also had the #[On] attribute to listen for an event.

See here for more details: https://livewire.laravel.com/docs/actions#skipping-re-renders

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.