Skip to content

Commit a1ae5fb

Browse files
authored
docs(reorder): assign stable identity to loop items (#3239)
1 parent bb4eaf6 commit a1ae5fb

File tree

5 files changed

+12
-4
lines changed

5 files changed

+12
-4
lines changed

docs/api/reorder.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,14 @@ import Wrapper from '@site/static/usage/v7/reorder/wrapper/index.md';
6161

6262
## Updating Data
6363

64-
When the `complete` method is called on the reorder group with no parameters, the DOM nodes will be reordered. If the items are rendered from an array of data that needs to be sorted, this can result in the data and DOM being out of sync. In order to sort the array upon completion of the reorder, the array should be passed as a parameter to the `complete` method. The `complete` method will sort the array and return it so it can be reassigned.
64+
When the `complete` method is called on the reorder group with no parameters, the DOM nodes will be reordered. If the items are rendered from an array of data that needs to be sorted, this can result in the data and DOM being out of sync.
65+
66+
In order to sort the array upon completion of the reorder, the array should be passed as a parameter to the `complete` method. The `complete` method will sort the array and return it so it can be reassigned. Note that passing the array will prevent Ionic from reordering the DOM nodes.
6567

6668
In some cases, it may be necessary for an app to reorder both the array and the DOM nodes on its own. If this is required, `false` should be passed as a parameter to the `complete` method. This will prevent Ionic from reordering any DOM nodes inside of the reorder group.
6769

70+
Regardless of the approach taken, a stable identity should be provided to reorder items if provided in a loop. This means using `trackBy` for Angular, and `key` for React and Vue.
71+
6872
import UpdatingData from '@site/static/usage/v7/reorder/updating-data/index.md';
6973

7074
<UpdatingData />

static/usage/v7/reorder/updating-data/angular/example_component_html.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<!-- The reorder gesture is disabled by default, enable it to drag and drop items -->
44
<!-- Casting $event to $any is a temporary fix for this bug https://github.com/ionic-team/ionic-framework/issues/24245 -->
55
<ion-reorder-group [disabled]="false" (ionItemReorder)="handleReorder($any($event))">
6-
<ion-item *ngFor="let item of items">
6+
<ion-item *ngFor="let item of items; trackBy: trackItems">
77
<ion-label> Item {{ item }} </ion-label>
88
<ion-reorder slot="end"></ion-reorder>
99
</ion-item>

static/usage/v7/reorder/updating-data/angular/example_component_ts.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,9 @@ export class ExampleComponent {
2323
// After complete is called the items will be in the new order
2424
console.log('After complete', this.items);
2525
}
26+
27+
trackItems(index: number, itemNumber: number) {
28+
return itemNumber;
29+
}
2630
}
2731
```

static/usage/v7/reorder/updating-data/react.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function Example() {
2424
{/* The reorder gesture is disabled by default, enable it to drag and drop items */}
2525
<IonReorderGroup disabled={false} onIonItemReorder={handleReorder}>
2626
{items.map((item) => (
27-
<IonItem>
27+
<IonItem key={item}>
2828
<IonLabel>Item {item}</IonLabel>
2929
<IonReorder slot="end"></IonReorder>
3030
</IonItem>

static/usage/v7/reorder/updating-data/vue.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<ion-list>
44
<!-- The reorder gesture is disabled by default, enable it to drag and drop items -->
55
<ion-reorder-group :disabled="false" @ionItemReorder="handleReorder($event)">
6-
<ion-item v-for="item in items">
6+
<ion-item v-for="item in items" :key="item">
77
<ion-label> Item {{ item }} </ion-label>
88
<ion-reorder slot="end"></ion-reorder>
99
</ion-item>

0 commit comments

Comments
 (0)