1e41f4b71Sopenharmony_ci# Repeat
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciWhen **virtualScroll** is disabled, the **Repeat** component, which is used together with the container component, performs loop rendering based on array data. In addition, the component returned by the API should be a child component that can be contained in the **Repeat** parent container. Compared with ForEach, **Repeat** optimizes the rendering performance in some update scenarios and generates function with the index maintained by the framework.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciWhen **virtualScroll** is enabled, **Repeat** iterates data from the provided data source as required and creates the corresponding component during each iteration. When **Repeat** is used in the scrolling container, the framework creates components as required based on the visible area of the scrolling container. When a component slides out of the visible area, the framework caches the component and uses it in the next iteration.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci>**NOTE**
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version.
10e41f4b71Sopenharmony_ci>
11e41f4b71Sopenharmony_ci> State management V2 is still under development, and some features may be incomplete or not always work as expected.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 12.
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci**Parameters**
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci| Name| Type      | Mandatory| Description     |
20e41f4b71Sopenharmony_ci| ------ | ---------- | -------- | -------- |
21e41f4b71Sopenharmony_ci| arr    | Array\<T\> | Yes| Data source, which is an array of the **Array\<T>** type. You can determine the data types.|
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**Return value**
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci| Type| Description|
26e41f4b71Sopenharmony_ci| --- | --- |
27e41f4b71Sopenharmony_ci| [RepeatAttribute](#repeatattribute)\<T\> | Repeat attributes.|
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci## RepeatAttribute
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci### each
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_cieach(itemGenerator: (repeatItem: RepeatItem\<T\>) => void): RepeatAttribute\<T\>
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ciComponent generator. Data items that do not match **template** and **templateId** are generated using the **each** function by default.
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci**NOTE**
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci- The **each** attribute is mandatory. Otherwise, an error occurs during running.
40e41f4b71Sopenharmony_ci- The parameter of **itemGenerator** is **RepeatItem**, which combines **item** and **index**. Do not split the **RepeatItem** and use it separately.
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 12.
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci**Parameters**
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci| Name| Type  | Mandatory| Description|
51e41f4b71Sopenharmony_ci| ------ | ---------- | -------- | -------- |
52e41f4b71Sopenharmony_ci| repeatItem  | [RepeatItem](#repeatitem)\<T\> | Yes| Repeat items.|
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci### key
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_cikey(keyGenerator: (item: T, index: number) => string): RepeatAttribute\<T\>
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ciKey generator.
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 12.
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci**Parameters**
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci| Name| Type  | Mandatory| Description |
69e41f4b71Sopenharmony_ci| ------ | ---------- | -------- | -------- |
70e41f4b71Sopenharmony_ci| item  | T | Yes| Data item in the **arr** array.|
71e41f4b71Sopenharmony_ci| index  | number | Yes| Index of a data item in the **arr** array.|
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci### virtualScroll
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_civirtualScroll(virtualScrollOptions?: VirtualScrollOptions): RepeatAttribute\<T\>
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ciEnables virtual scrolling for the **Repeat** component.
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 12.
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci**Parameters**
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci| Name| Type  | Mandatory| Description |
88e41f4b71Sopenharmony_ci| ------ | ---------- | -------- | -------- |
89e41f4b71Sopenharmony_ci| virtualScrollOptions  | [VirtualScrollOptions](#virtualscrolloptions)  | No| Virtual scrolling configuration.|
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci### template
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_citemplate(type: string, itemBuilder: RepeatItemBuilder\<T\>, templateOptions?: TemplateOptions): RepeatAttribute\<T\>
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ciDefines a template for reusing.
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 12.
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci**Parameters**
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci| Name| Type  | Mandatory| Description |
106e41f4b71Sopenharmony_ci| ------ | ---------- | -------- | -------- |
107e41f4b71Sopenharmony_ci| type | string | Yes| Template type.|
108e41f4b71Sopenharmony_ci| itemBuilder  | [RepeatItemBuilder](#repeatitembuilder)\<T\> | Yes| Component generator.|
109e41f4b71Sopenharmony_ci| templateOptions | [TemplateOptions](#templateoptions) | No| Template configuration.|
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci### templateId
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_citemplateId(typedFunc: TemplateTypedFunc\<T\>): RepeatAttribute\<T\>
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ciAssigns a template ID to this data item.
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 12.
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci**Parameters**
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci| Name| Type  | Mandatory| Description |
126e41f4b71Sopenharmony_ci| ------ | ---------- | -------- | -------- |
127e41f4b71Sopenharmony_ci| typedFunc | [TemplateTypedFunc](#templatetypedfunc)\<T\> | Yes| Template ID to assign.|
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci## RepeatItem
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 12.
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci**Parameters**
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ci| Name| Type  | Mandatory| Description                                        |
140e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------------------------- |
141e41f4b71Sopenharmony_ci| item   | T      | Yes  | Each data item in **arr**. **T** indicates the data type passed in.|
142e41f4b71Sopenharmony_ci| index  | number | Yes  | Index corresponding to the current data item.                      |
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci## VirtualScrollOptions
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 12.
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci**Parameters**
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci| Name    | Type  | Mandatory| Description                                                        |
155e41f4b71Sopenharmony_ci| ---------- | ------ | ---- | ------------------------------------------------------------ |
156e41f4b71Sopenharmony_ci| totalCount | number | No  | Total length of the data source, which can be greater than the number of loaded data items.<br>With **arr.length** representing the length of the data source, the following are the rules for determining **totalCount**:<br>1. If **totalCount** is not set \|\| **totalCount** is not an integer \|\| totalCount <= 0 \|\| totalCount == arr.length, then **totalCount** is the length of the data source, and the list scrolls properly.<br>2. If 0 < **totalCount** < arr.length, only the **totalCount**-specified number of data items are rendered in the UI.<br>3. If **totalCount** > **arr.Length**, the scrollbar displays normally, but the areas without data items show up as empty spaces. When the scrolling animation finishes, the scrollbar rests at the position of the last data item. In this way, you can use the correct scrollbar style without requesting all the data at the same time.|
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci> **NOTE**<br>When **totalCount** < **arr.length**, you are advised to set **totalCount** to a value that can fill the parent component to prevent any empty spaces within the parent component, which could lead to a suboptimal user experience.
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci## RepeatItemBuilder
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_citype RepeatItemBuilder\<T\> = (repeatItem: RepeatItem\<T\>) => void
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 12.
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci**Parameters**
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_ci| Name    | Type         | Mandatory     | Description                                   |
173e41f4b71Sopenharmony_ci| ---------- | ------------- | --------------------------------------- | --------------------------------------- |
174e41f4b71Sopenharmony_ci| repeatItem | [RepeatItem](#repeatitem)\<T\> | Yes| A state variable that combines **item** and **index**.|
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci## TemplateOptions
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 12.
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci**Parameters**
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci| Name     | Type  | Mandatory| Description                                                        |
187e41f4b71Sopenharmony_ci| ----------- | ------ | ---- | ------------------------------------------------------------ |
188e41f4b71Sopenharmony_ci| cachedCount | number | No  | Maximum number of child nodes of the current template that can be cached in the **Repeat** cache pool. This parameter takes effect only when **virtualScroll** is enabled.<br>When **cachedCount** is set to the maximum number of nodes of the current template that may appear on the screen, **Repeat** can be reused as much as possible. However, when there is no node of the current template on the screen, the cache pool is not released and the application memory increases. You need to set the configuration based on the actual situation.<br>If you do not specify **cachedCount**, the framework calculates **cachedCount** for different templates based on the number of visible and pre-rendered nodes on the screen. When the number of visible and pre-rendered nodes on the screen increases, **cachedCount** will also increase accordingly. It should be noted that the value of **cachedCount** will not decrease.<br> To explicitly specify **cachedCount**, you are advised to set it to the same as the number of nodes on the screen. Yet, setting **cachedCount** to less than 2 is not advised. Doing so may lead to the creation of new nodes during rapid scrolling, which could result in performance degradation.|
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ci## TemplateTypedFunc
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_citype TemplateTypedFunc\<T\> = (item : T, index : number) => string
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 12.
195e41f4b71Sopenharmony_ci
196e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
197e41f4b71Sopenharmony_ci
198e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci**Parameters**
201e41f4b71Sopenharmony_ci
202e41f4b71Sopenharmony_ci| Name| Type  | Mandatory| Description                                        |
203e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------------------------- |
204e41f4b71Sopenharmony_ci| item   | T      | Yes  | Each data item in **arr**. **T** indicates the data type passed in.|
205e41f4b71Sopenharmony_ci| index  | number | Yes  | Index corresponding to the current data item.                      |
206