Skip to content

[LiveComponents] Child component cannot read URL parameters if the parent component is defered #2690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
momocode-de opened this issue Apr 11, 2025 · 2 comments
Labels
Bug Bug Fix Status: Needs Review Needs to be reviewed

Comments

@momocode-de
Copy link

I have noticed that if I have nested components and the parent component is included with loading: 'defer', the child component can no longer read URL parameters. Here is my code:

Parent Component

<?php

namespace App\Twig\Components;

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent]
class ParentComponent
{
    use DefaultActionTrait;
}
<div {{ attributes }}>
        <h1>Parent Component</h1>

        {{ component('ChildComponent', {
                sort: 'name'
        }) }}
</div>

Child Component

<?php

namespace App\Twig\Components;

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent]
class ChildComponent
{
    use DefaultActionTrait;

    #[LiveProp(writable: true, url: true)]
    public string $sort = '';
}
<div {{ attributes }}>
        <h1>Child Component</h1>

        {{ dump(sort) }}
</div>

Main template:

{{ component('ParentComponent', { loading: 'defer' }) }}

If I now call up my page with ?sort=foo, the value “name” is dumped instead of “foo”. If I remove the { loading: 'defer' } from the parent component, the value “foo” is dumped. Is it possible in this scenario that the parent component remains defered and the child component can still read the URL parameters?

@momocode-de momocode-de added the Bug Bug Fix label Apr 11, 2025
@carsonbot carsonbot added the Status: Needs Review Needs to be reviewed label Apr 11, 2025
@smnandre
Copy link
Member

If the parent is deferrred, the rendering of its children is deferred too.

They are initialized with the value passed by their parent: name

@momocode-de
Copy link
Author

momocode-de commented Apr 19, 2025

So is it intentional that the children cannot use URL parameters in this case? Should then be mentioned in the documentation.

But you could solve it by checking in the QueryStringPropsExtractor at the beginning whether the current route is ux_live_component (this is the case for the children in my example). If it is, you could then extract the query string from the referer URL instead of from the current URL. Or is there something against this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix Status: Needs Review Needs to be reviewed
Projects
None yet
Development

No branches or pull requests

3 participants