Skip to content

[LiveComponent] How "defer" option work and the logic behind __invoke #1543

Closed
@gremo

Description

@gremo

I have a WheaterWidget with a refresh action that can be invoked with the data-pool option or a button click:

#[AsLiveComponent]
class WheaterWidget
{
    public function __invoke(): void
    {
        $this->refresh();
    }

    #[LiveAction]
    public function refresh(): void
    {
        // Update component internal state
    }
}

Here is the behaviour:

  1. With <twig:WheaterWidget defer /> method __invoke is called -> widget is refreshed as soon as it loads
  2. With <twig:WheaterWidget /> method __invoke isn't called -> widget isn't updated

I want to make it work (refreshed as soon as it loads) with or without defer. So I added a [PostMount] attribute to refresh:

#[LiveAction]
#[PostMount]
public function refresh(): void
{
    // Update component internal state
}

And this, indeed, make the widget behave correcly for case 2... but then adding a defer will make it refresh twice (both __invoke and refresh are called).

So what's the correct way to handle this kind of situation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions