Skip to content

[Doc] Update the docs about the mount() method of Twig components #2843

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
wants to merge 2 commits into
base: 2.x
Choose a base branch
from

Conversation

javiereguiluz
Copy link
Member

Q A
Bug fix? no
New feature? no
Docs? yes
Issues -
License MIT

Today I had some issues understanding how the mount() method works, so I propose these changes to clarify things.

@javiereguiluz javiereguiluz added docs Improvements or additions to documentation TwigComponent labels Jun 16, 2025
@carsonbot carsonbot added the Status: Needs Review Needs to be reviewed label Jun 16, 2025
Comment on lines +450 to +452
Most of the time, you will create public properties and pass values to them
as "props" when rendering the component. However, there are several hooks
available when you need to perform more complex logic.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

Comment on lines +457 to +458
The ``mount()`` method gives you more control over how your "props" are handled.
It is called only once: immediately after your component is instantiated.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

Comment on lines +459 to +460
The ``mount()`` method **cannot access the values of the public properties**
passed when rendering the component.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean, but this is wrong if I want to be picky:

  1. you can access the public properties values, if they are not typed or have a default values
  2. speaking about only "public properties values" can be misleading, I feel like we should speak about the "props" too in priority

{
{# ✅ this works as expected #}
if ($isError) {
// ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like it was done before, we should keep an example (or document) that we can modify a property here

Suggested change
// ...
$this->type = 'danger';

Comment on lines +510 to +514
Now, pass the ``mount()`` argument like any other prop:

.. code-block:: html+twig

<twig:Alert
isSuccess="{{ false }}"
message="Danger Will Robinson!"
/>
<twig:Alert isError="{{ true }}" message="..."/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move it just under Instead, define the values you need in the ``mount()`` method as arguments::? I feel like the actual sections order can be misleading

component is instantiated. Because the method has an ``$isSuccess``
argument, if we pass an ``isSuccess`` prop when rendering, it will be
passed to ``mount()``.
Instead, define the values you need in the ``mount()`` method as arguments::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameters you define in mount() does not only refer to your Component's public properties, but can be any props. For example, if I use <twig:Alert toto="tata">, then I can retrieve its value with public function mount(string $toto):

Suggested change
Instead, define the values you need in the ``mount()`` method as arguments::
Instead, the props values are available in the ``mount()`` method as arguments::

@Kocal Kocal added Status: Needs Work Additional work is needed and removed Status: Needs Review Needs to be reviewed labels Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to documentation Status: Needs Work Additional work is needed TwigComponent
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants