Skip to content

Commit 6130203

Browse files
committed
fixes
1 parent 3b326f6 commit 6130203

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

src/LiveComponent/doc/index.rst

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,7 +2639,7 @@ understand which element is which between re-renders (i.e. if something re-order
26392639
or removes some of those elements), you can add a ``data-live-id`` attribute to
26402640
each element
26412641

2642-
.. code-block:: twig
2642+
.. code-block:: html+twig
26432643

26442644
{# templates/components/Invoice.html.twig #}
26452645
{% for lineItem in lineItems %}
@@ -2665,9 +2665,9 @@ to that component:
26652665

26662666
.. code-block:: twig
26672667
2668-
{# templates/components/Invoice.html.twig #}
2669-
{% for lineItem in lineItems %}
2670-
{{ component('InvoiceLineItem', {
2668+
{# templates/components/InvoiceCreator.html.twig #}
2669+
{% for lineItem in invoice.lineItems %}
2670+
{{ component('InvoiceLineItemForm', {
26712671
lineItem: lineItem,
26722672
key: lineItem.id,
26732673
}) }}
@@ -2681,32 +2681,34 @@ a bit more convenient.
26812681
Tricks with a Loop + a "New" Item
26822682
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26832683

2684-
Let's get fancier. Suppose you're rendering all of the line items
2685-
inside of an `InvoiceCreator` component. After the loop, you might
2684+
Let's get fancier. After looping over the current line items, you
26862685
decide to render one more component to create a *new* line item.
26872686
In that case, you can pass in a ``key`` set to something like ``new_line_item``:
26882687

2689-
.. code-block:: twig
2688+
.. code-block:: html+twig
26902689

2691-
{{ component('InvoiceLineItem', {
2690+
{# templates/components/InvoiceCreator.html.twig #}
2691+
// ... loop and render the existing line item components
2692+
2693+
{{ component('InvoiceLineItemForm', {
26922694
key: 'new_line_item',
26932695
}) }}
26942696

2695-
Imagine you also have a ``LiveAction`` inside of ``InvoiceLineItem``
2697+
Imagine you also have a ``LiveAction`` inside of ``InvoiceLineItemForm``
26962698
that saves the new line item to the database. To be extra fancy,
26972699
it emits a ``lineItem:created`` event to the parent::
26982700

2699-
// src/Twig/InvoiceLineItem.php
2701+
// src/Twig/InvoiceLineItemForm.php
27002702
// ...
27012703

27022704
#[AsLiveComponent]
2703-
final class InvoiceLineItem
2705+
final class InvoiceLineItemForm
27042706
{
27052707
// ...
27062708

27072709
#[LiveProp]
27082710
#[Valid]
2709-
public InvoiceLineItem $lineItem;
2711+
public ?InvoiceLineItem $lineItem = null;
27102712

27112713
#[PostMount]
27122714
public function postMount(): void
@@ -2760,22 +2762,21 @@ component on the page is the *same* new component that it's about to render. And
27602762
because the props passed into that component haven't changed, it doesn't see any
27612763
reason to re-render it.
27622764

2763-
To fix this, you haev two options:
2765+
To fix this, you have two options:
27642766

27652767
1. Make the ``key`` dynamic so it will be different after adding a new item::
27662768

27672769
.. code-block:: twig
27682770
2769-
{{ component('InvoiceLineItem', {
2770-
key: 'new_line_item_' lineItems|length,
2771+
{{ component('InvoiceLineItemForm', {
2772+
key: 'new_line_item_'~lineItems|length,
27712773
}) }}
27722774
2773-
2. Reset the state of the ``InvoiceLineItem`` component after it's saved::
2774-
2775-
// src/Twig/InvoiceLineItem.php
2775+
2. Reset the state of the ``InvoiceLineItemForm`` component after it's saved::
27762776

2777+
// src/Twig/InvoiceLineItemForm.php
27772778
// ...
2778-
class InvoiceLineItem
2779+
class InvoiceLineItemForm
27792780
{
27802781
// ...
27812782

0 commit comments

Comments
 (0)