ux: [LiveComponent] Stimulus controller inside a Live Component does not rerender
Hello,
I have a Live Component containing a search form and a table of results dynamically generated by an embedded stimulus controller.
When I type something in the form, the Live Component is re-rendered as expected, but nothing happens on the child stimulus controller. After reading this issue I added a data-live-id attribute on the controller but it does not solve my problem.
I can see the HTML being succesfully updated by the live component, but the stimulus controller doesn’t seem to react to that change at all.
Did I forget something here?
#[AsLiveComponent]
class AdminUsers extends AbstractController
{
use DefaultActionTrait;
use ComponentWithFormTrait;
#[LiveProp]
public array $initialFormData = [];
#[LiveProp]
public array $users = [];
public function mount(): void
{
$this->users = $this->fetchUsers($this->initialFormData);
}
public function __invoke(): void
{
$this->search();
}
#[LiveAction]
public function search(): void
{
$this->submitForm();
$this->users = $this->fetchUsers($this->getForm()->getData());
}
protected function fetchUsers(array $searchData = []): array
{
// fetching users from database using $searchData to filter results...
return $users;
}
protected function instantiateForm(): FormInterface
{
return $this->createForm(
type : AdminUsersSearchType::class,
data : $this->initialFormData,
);
}
}
<div {{ attributes }}>
<twig:UI:Section :full="false">
<twig:UI:Title:Section icon="fa6-solid:magnifying-glass">Rechercher</twig:UI:Title:Section>
{{ form_start(form, {
method: 'POST',
attr: {
'data-model': 'debounce(300)|*',
'data-action': 'live#action',
'data-live-action-param': 'search',
},
}) }}
<div class="form_grid form_grid-2_columns">
{% for child in form %}
{% if child.vars.name != '_token' %}
<div class="form_label">
{{ form_label(child) }}
</div>
<div class="form_field">
{{ form_widget(child) }}
</div>
{% endif %}
{% endfor %}
<div class="form_row form_row-buttons padding-top_s">
{% block buttons %}
<input type="submit" value="Rechercher">
{% endblock %}
</div>
</div>
{{ form_rest(form) }}
{{ form_end(form) }}
</twig:UI:Section>
<twig:UI:Section data-live-id="{{ microtime() }}">
{# The title below is correctly updated each time the Live Component re-renders
(so I see the correct number of results according to what I typed in the search form above) #}
<twig:UI:Title:Section>
{{ users|length~(users|length > 1 ? ' Résultats' : ' Résultat') }}
</twig:UI:Title:Section>
{# The markup of the stimulus controller below is correctly updated each time the Live Component re-renders,
but the controller is actually never re-rendered (and so the generated table never changes) #}
<div id="users_table"
data-controller="UI--datatable"
data-UI--datatable-data-value='{{ users|json_encode }}'
data-UI--datatable-columns-value='{{ [
{
title: 'E-mail',
name: 'email',
width: 250,
},
{
title: 'Identifiant',
name: 'username',
width: 250,
},
]|json_encode }}'
data-live-id="{{ microtime() }}"
class="datatable"
>
<div data-UI--datatable-target="table" class="datatable_table"></div>
</div>
</twig:UI:Section>
</div>
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 17 (9 by maintainers)
@smnandre Sorry for the delay.
Since last time I moved on other things and now it’s not easy to find enough time to focus on this issue again. The workaround suggested by @CMH-Benny is good enough for me, so I’m closing this issue.
If I’m stuck with something similar in the future I may open a new issue, in that case I’ll add a link to this one. But at the moment everything is good for me.
Thanks again everyone for your help.
This works actually, thanks @CMH-Benny !
@smnandre if you’re still interested by a reproducer I’ll do it, but I’m not sure I’ll have the time today sorry, it may be only next week