[Doc] Update the docs about the mount() method of Twig components#2843
Conversation
| 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. |
| The ``mount()`` method gives you more control over how your "props" are handled. | ||
| It is called only once: immediately after your component is instantiated. |
| The ``mount()`` method **cannot access the values of the public properties** | ||
| passed when rendering the component. |
There was a problem hiding this comment.
I see what you mean, but this is wrong if I want to be picky:
- you can access the public properties values, if they are not typed or have a default values
- 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) { | ||
| // ... |
There was a problem hiding this comment.
Like it was done before, we should keep an example (or document) that we can modify a property here
| // ... | |
| $this->type = 'danger'; |
| 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="..."/> |
There was a problem hiding this comment.
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:: |
There was a problem hiding this comment.
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):
| Instead, define the values you need in the ``mount()`` method as arguments:: | |
| Instead, the props values are available in the ``mount()`` method as arguments:: |
|
Thanks for the review Hugo! I reworded some parts. Hopefully it's better now. |
|
Thanks for this nice improvment, Javier :) |
Today I had some issues understanding how the
mount()method works, so I propose these changes to clarify things.