Skip to content

Commit 4066e13

Browse files
committed
Adding section about data-live-id
1 parent 6130203 commit 4066e13

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/LiveComponent/doc/index.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,6 +2350,8 @@ a model in a child updates, it won't also update that model in its parent
23502350
The parent-child system is *smart*. And with a few tricks, you can make
23512351
it behave exactly like you need.
23522352

2353+
.. _child-component-independent-rerender:
2354+
23532355
Each component re-renders independent of one another
23542356
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23552357

@@ -2631,6 +2633,8 @@ Notice that ``MarkdownTextarea`` allows a dynamic ``name``
26312633
attribute to be passed in. This makes that component re-usable in any
26322634
form.
26332635

2636+
.. _rendering-loop-of-elements:
2637+
26342638
Rendering Quirks with List of Elements
26352639
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26362640

@@ -2648,6 +2652,8 @@ each element
26482652
</div>
26492653
{% endfor %}
26502654

2655+
.. _key-prop:
2656+
26512657
Rendering Quirks with List of Embedded Components
26522658
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26532659

@@ -2678,6 +2684,8 @@ which will be used to identify each child component. You can
26782684
also pass in a ``data-live-id`` attribute directly, but ``key`` is
26792685
a bit more convenient.
26802686

2687+
.. _rendering-loop-new-element:
2688+
26812689
Tricks with a Loop + a "New" Item
26822690
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26832691

@@ -2831,6 +2839,40 @@ The system doesn't handle every edge case, so here are some things to keep in mi
28312839
that change is **lost**: the element will be re-added in its original location
28322840
during the next re-render.
28332841

2842+
The Mystical data-live-id Attribute
2843+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2844+
2845+
The ``data-live-id`` attribute is mentioned several times throughout the documentation
2846+
to solve various problems. It's usually not needed, but can be the key to solving
2847+
certain complex problems. But what is it?
2848+
2849+
.. note::
2850+
2851+
The :ref:`key prop <key-prop>` is used to create a ``data-live-id`` attribute
2852+
on child components. So everything in this section applies equally to the
2853+
``key`` prop.
2854+
2855+
The ``data-live-id`` attribute is a unique identifier for an element or a component.
2856+
It's used when a component re-renders and helps Live Components "connect" elements
2857+
or components in the existing HTML with the new HTML. The logic works like this:
2858+
2859+
Suppose an element or component in the new HTML has a ``data-live-id="some-id`` attribute.
2860+
Then:
2861+
2862+
A) If there **is** an element or component with ``data-live-id="some-id"`` in the
2863+
existing HTML, then the old and new elements/components are considered to be the
2864+
"same". For elements, the new element will be used to update the old element even
2865+
if the two elements appear in different places - e.g. like if :ref:`elements are moved <rendering-loop-of-elements>`
2866+
or re-ordered. For components, because child components render independently
2867+
from their parent, the existing component will be "left alone" and not re-rendered
2868+
(unless some ``updateFromParent`` props have changed - see :ref:`child-component-independent-rerender`).
2869+
2870+
B) If there is **not** an element or component with ``data-live-id="some-id"`` in
2871+
the existing HTML, then the new element or component is considered to be "new".
2872+
In both cases, the new element or component will be added to the page. If there
2873+
is a component/element with a ``data-live-id`` attribute that is *not* in the
2874+
new HTML, that component/element will be removed from the page.
2875+
28342876
Skipping Updating Certain Elements
28352877
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28362878

0 commit comments

Comments
 (0)