• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Reuse Options
2
3The **reuse** attribute is used to specify reuse options for custom components decorated with @ReusableV2.
4
5This document is solely for API reference. For details about the usage guidelines and constraints, see [@ReusableV2 Decorator: Reusing Components](../../../ui/state-management/arkts-new-reusableV2.md).
6
7>  **NOTE**
8>
9> This feature is supported since API version 18. Updates will be marked with a superscript to indicate their earliest API version.
10
11## reuse
12
13reuse(options: ReuseOptions)
14
15Sets the reuse options for custom components decorated with @ReusableV2.
16
17**Atomic service API**: This API can be used in atomic services since API version 18.
18
19**System capability**: SystemCapability.ArkUI.ArkUI.Full
20
21**Parameters**
22
23| Name | Type                         | Mandatory| Description                                          |
24| ------- | ----------------------------- | ---- | ---------------------------------------------- |
25| options | [ReuseOptions](#reuseoptions) | Yes  | Custom reuse configuration.|
26
27## ReuseOptions
28
29Defines the reuse options.
30
31**Atomic service API**: This API can be used in atomic services since API version 18.
32
33**System capability**: SystemCapability.ArkUI.ArkUI.Full
34
35### Properties
36
37| Name   | Type                               | Mandatory| Description                                                        |
38| ------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
39| reuseId | [ReuseIdCallback](#reuseidcallback) | No  | Reuse ID. Components with same **reuseId** will be mutually reused. The default reuse ID is the component name.|
40
41## ReuseIdCallback
42
43type ReuseIdCallback = () => string
44
45Defines the callback used to obtain the reuse ID.
46
47**Atomic service API**: This API can be used in atomic services since API version 18.
48
49**System capability**: SystemCapability.ArkUI.ArkUI.Full
50
51**Return value**
52
53| Type  | Description                                                        |
54| ------ | ------------------------------------------------------------ |
55| string | Developer-specified reuse ID.<br>It uses the component name if unspecified or if an empty string **''** is provided.|
56
57## Example
58
59```ts
60@Entry
61@ComponentV2
62struct Index {
63  build() {
64    Column() {
65      ReusableV2Component()
66        .reuse({reuseId: () => 'reuseComponent'}) // Use 'reuseComponent' as reuseId.
67      ReusableV2Component()
68        .reuse({reuseId: () => ''}) // If an empty string is used, the component name 'ReusableV2Component' is used as reuseId.
69      ReusableV2Component() // If reuseId is not specified, the component name 'ReusableV2Component' is used as reuseId.
70    }
71  }
72}
73@ReusableV2
74@ComponentV2
75struct ReusableV2Component {
76  build() {
77  }
78}
79```
80