@@ -294,11 +294,8 @@ PostMount Hook
294
294
295
295
The ``PostMount `` hook was added in TwigComponents 2.1.
296
296
297
- When a component is mounted with the passed data, if an item cannot be
298
- mounted on the component, an exception is thrown. You can intercept this
299
- behavior and "catch" this extra data with a ``PostMount `` hook method. This
300
- method accepts the extra data as an argument and must return an array. If
301
- the returned array is empty, the exception will be avoided::
297
+ After a component is instantiated and its data mounted, you can run extra
298
+ code via the ``PostMount `` hook::
302
299
303
300
// src/Components/Alert.php
304
301
use Symfony\UX\TwigComponent\Attribute\PostMount;
@@ -319,18 +316,8 @@ the returned array is empty, the exception will be avoided::
319
316
320
317
A ``PostMount `` method can also receive an array ``$data `` argument, which
321
318
will contain any props passed to the component that have *not * yet been processed,
322
- i.e. because they don't correspond to any property. You can handle and remove those
323
- here. For example, imagine an extra ``autoChooseType `` prop were passed when
324
- creating the ``Alert `` component:
325
-
326
- .. code-block :: twig
327
-
328
- {{ component('Alert', {
329
- message: 'Danger Will Robinson!',
330
- autoChooseType: true,
331
- }) }}
332
-
333
- You can handle this prop via a ``#[PostMount] `` hook::
319
+ i.e. because they don't correspond to any property. You can handle these props,
320
+ remove them from the ``$data `` and return the array::
334
321
335
322
// src/Components/Alert.php
336
323
#[AsTwigComponent]
@@ -342,7 +329,7 @@ You can handle this prop via a ``#[PostMount]`` hook::
342
329
#[PostMount]
343
330
public function processAutoChooseType(array $data): array
344
331
{
345
- if (array_key_exists('autoChooseType', $data) && $data ['autoChooseType']) {
332
+ if ($data['autoChooseType'] ?? false ) {
346
333
if (str_contains($this->message, 'danger')) {
347
334
$this->type = 'danger';
348
335
}
0 commit comments