-
-
Notifications
You must be signed in to change notification settings - Fork 364
[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
base: 2.x
Are you sure you want to change the base?
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻
The ``mount()`` method gives you more control over how your "props" are handled. | ||
It is called only once: immediately after your component is instantiated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻
The ``mount()`` method **cannot access the values of the public properties** | ||
passed when rendering the component. |
There was a problem hiding this comment.
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:
- 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.
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
// ... | |
$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.
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:: |
There was a problem hiding this comment.
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)
:
Instead, define the values you need in the ``mount()`` method as arguments:: | |
Instead, the props values are available in the ``mount()`` method as arguments:: |
Today I had some issues understanding how the
mount()
method works, so I propose these changes to clarify things.