Skip to content

Commit ce1bcfb

Browse files
committed
minor #883 [TwigComponent] Clarifying PostMount hook (weaverryan)
This PR was merged into the 2.x branch. Discussion ---------- [TwigComponent] Clarifying PostMount hook | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Tickets | None | License | MIT From a conversation on Slack with `@mina20088`. The previous text was accurate... but it made a big deal out of a feature that few will need. Really, you could use `mount()` instead of `PostMount` I think in all cases. `PostMount` IS still nice if you want to run some processing AFTER all of your properties have been mounted. Cheers! Commits ------- b9ec3f7 [TwigComponent] Clarifying PostMount hook
2 parents feb8a27 + b9ec3f7 commit ce1bcfb

File tree

1 file changed

+5
-18
lines changed

1 file changed

+5
-18
lines changed

src/TwigComponent/doc/index.rst

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,8 @@ PostMount Hook
294294

295295
The ``PostMount`` hook was added in TwigComponents 2.1.
296296

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::
302299

303300
// src/Components/Alert.php
304301
use Symfony\UX\TwigComponent\Attribute\PostMount;
@@ -319,18 +316,8 @@ the returned array is empty, the exception will be avoided::
319316

320317
A ``PostMount`` method can also receive an array ``$data`` argument, which
321318
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::
334321

335322
// src/Components/Alert.php
336323
#[AsTwigComponent]
@@ -342,7 +329,7 @@ You can handle this prop via a ``#[PostMount]`` hook::
342329
#[PostMount]
343330
public function processAutoChooseType(array $data): array
344331
{
345-
if (array_key_exists('autoChooseType', $data) && $data['autoChooseType']) {
332+
if ($data['autoChooseType'] ?? false) {
346333
if (str_contains($this->message, 'danger')) {
347334
$this->type = 'danger';
348335
}

0 commit comments

Comments
 (0)