diff --git a/src/TwigComponent/src/ComponentAttributes.php b/src/TwigComponent/src/ComponentAttributes.php
index 751e324306e..574a385d69e 100644
--- a/src/TwigComponent/src/ComponentAttributes.php
+++ b/src/TwigComponent/src/ComponentAttributes.php
@@ -149,8 +149,12 @@ public function add($stimulusDto): self
return $clone->defaults($controllersAttributes);
}
- public function remove($key): void
+ public function remove($key): self
{
- unset($this->attributes[$key]);
+ $attributes = $this->attributes;
+
+ unset($attributes[$key]);
+
+ return new self($attributes);
}
}
diff --git a/src/TwigComponent/src/Twig/PropsNode.php b/src/TwigComponent/src/Twig/PropsNode.php
index 17aebc43ef7..250b0ea2be1 100644
--- a/src/TwigComponent/src/Twig/PropsNode.php
+++ b/src/TwigComponent/src/Twig/PropsNode.php
@@ -31,7 +31,7 @@ public function compile(Compiler $compiler): void
foreach ($this->getAttribute('names') as $name) {
$compiler
->addDebugInfo($this)
- ->write('$context[\'attributes\']->remove(\''.$name.'\');')
+ ->write('$context[\'attributes\'] = $context[\'attributes\']->remove(\''.$name.'\');')
->write('if (!isset($context[\''.$name.'\'])) {')
;
diff --git a/src/TwigComponent/tests/Fixtures/templates/anonymous_component_none_scalar_prop.html.twig b/src/TwigComponent/tests/Fixtures/templates/anonymous_component_none_scalar_prop.html.twig
index a1c267e1f1c..4bbccfb3fb7 100644
--- a/src/TwigComponent/tests/Fixtures/templates/anonymous_component_none_scalar_prop.html.twig
+++ b/src/TwigComponent/tests/Fixtures/templates/anonymous_component_none_scalar_prop.html.twig
@@ -1 +1 @@
-
{{ user.name }}
{{ user.email }}