Skip to content

Commit ed2ca6c

Browse files
committed
[Doc] Update the docs about the mount() method of Twig components
1 parent ae07fdc commit ed2ca6c

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

src/TwigComponent/doc/index.rst

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -447,15 +447,25 @@ need to populate, you can render it with:
447447
Mounting Data
448448
-------------
449449

450-
Most of the time, you will create public properties and then pass values
451-
to those as "props" when rendering. But there are several hooks in case
452-
you need to do something more complex.
450+
Most of the time, you will create public properties and pass values to them
451+
as "props" when rendering the component. However, there are several hooks
452+
available when you need to perform more complex logic.
453453

454454
The mount() Method
455455
~~~~~~~~~~~~~~~~~~
456456

457-
For more control over how your "props" are handled, you can create a method
458-
called ``mount()``::
457+
The ``mount()`` method gives you more control over how your "props" are handled.
458+
It is called only once: immediately after your component is instantiated.
459+
The ``mount()`` method **cannot access the values of the public properties**
460+
passed when rendering the component.
461+
462+
For example, if you call your component like this:
463+
464+
.. code-block:: twig
465+
466+
<twig:Alert type="error" message="..."/>
467+
468+
The following code won't work as expected:
459469

460470
// src/Twig/Components/Alert.php
461471
// ...
@@ -466,29 +476,46 @@ called ``mount()``::
466476
public string $message;
467477
public string $type = 'success';
468478

469-
public function mount(bool $isSuccess = true): void
479+
public function mount(): void
470480
{
471-
$this->type = $isSuccess ? 'success' : 'danger';
481+
{# ❌ this won't work: at this point, the $type property still has its
482+
default value. Passed properties are not yet available. #}
483+
if ('error' === $this->type) {
484+
// ...
485+
}
472486
}
473487

474488
// ...
475489
}
476490

477-
The ``mount()`` method is called just one time: immediately after your
478-
component is instantiated. Because the method has an ``$isSuccess``
479-
argument, if we pass an ``isSuccess`` prop when rendering, it will be
480-
passed to ``mount()``.
491+
Instead, define the values you need in the ``mount()`` method as arguments::
481492

482-
.. code-block:: html+twig
493+
#[AsTwigComponent]
494+
class Alert
495+
{
496+
public string $message;
497+
public string $type = 'success';
483498

484-
<twig:Alert
485-
isSuccess="{{ false }}"
486-
message="Danger Will Robinson!"
487-
/>
499+
public function mount(bool $isError = false): void
500+
{
501+
{# ✅ this works as expected #}
502+
if ($isError) {
503+
// ...
504+
}
505+
}
506+
507+
// ...
508+
}
509+
510+
Now, pass the ``mount()`` argument like any other prop:
511+
512+
.. code-block:: twig
513+
514+
<twig:Alert isError="{{ true }}" message="..."/>
488515
489-
If a prop name (e.g. ``isSuccess``) matches an argument name in ``mount()``,
490-
the prop will be passed as that argument and the component system will
491-
**not** try to set it directly on a property or use it for the component
516+
If a prop name (e.g. ``isError``) matches an argument name in ``mount()``,
517+
its value will be passed to the method. In that case, the component system
518+
**will not** set it on a public property or use it in the component's
492519
``attributes``.
493520

494521
PreMount Hook

0 commit comments

Comments
 (0)