@@ -447,15 +447,25 @@ need to populate, you can render it with:
447
447
Mounting Data
448
448
-------------
449
449
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 .
453
453
454
454
The mount() Method
455
455
~~~~~~~~~~~~~~~~~~
456
456
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:
459
469
460
470
// src/Twig/Components/Alert.php
461
471
// ...
@@ -466,29 +476,46 @@ called ``mount()``::
466
476
public string $message;
467
477
public string $type = 'success';
468
478
469
- public function mount(bool $isSuccess = true ): void
479
+ public function mount(): void
470
480
{
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
+ }
472
486
}
473
487
474
488
// ...
475
489
}
476
490
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::
481
492
482
- .. code-block :: html+twig
493
+ #[AsTwigComponent]
494
+ class Alert
495
+ {
496
+ public string $message;
497
+ public string $type = 'success';
483
498
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="..."/>
488
515
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
492
519
``attributes ``.
493
520
494
521
PreMount Hook
0 commit comments