• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# AttributeUpdater
2
3**AttributeUpdater** directly set attributes to a component to trigger UI re-renders, without marking them as state variables.
4
5>  **NOTE**
6>
7>  The initial APIs of this module are supported since API version 12. Updates will be marked with a superscript to indicate their earliest API version.
8>
9
10
11## Modules to Import
12
13```ts
14import { AttributeUpdater } from '@kit.ArkUI'
15```
16
17>  **Instructions**
18>
19>  1. Whenever possible, avoid using **AttributeUpdater** in conjunction with attribute methods or implementing methods like **applyNormalAttribute** in **AttributeUpdater** to update the same attribute on the same component. Doing so involves mixed use of the state management update mechanism, which can be confusing.
20>
21>  2. When **AttributeUpdater** is used together with an attribute method, the one that is used later takes effect. Specifically:
22>  If use of **AttributeUpdater** is followed by an attribute method call, the attribute method takes effect under the state management mechanism.
23>  If use of **AttributeUpdater** follows an attribute method call, it takes effect.
24>
25>  3. An **AttributeUpdater** object can only be associated with one component at a time; otherwise, the set attributes may only take effect on one component.
26>
27>  4. You need to ensure the type matching of **T** and **C** in **AttributeUpdater** yourself. For example, if **T** is ImageAttribute, **C** should be ImageInterface;
28>  otherwise, it may cause functionality issues when **updateConstructorParams** is used.
29>
30>  5. Currently, **updateConstructorParams** supports only the **Button**, **Image**, **Text**, and **Span** components.
31
32## Initializer
33type Initializer\<T> = () => T
34
35Defines a decorator for updating attributes.
36
37**Atomic service API**: This API can be used in atomic services since API version 12.
38
39**System capability**: SystemCapability.ArkUI.ArkUI.Full
40
41## AttributeUpdater<T, C = Initializer\<T>>
42Represents the implementation class of [AttributeModifier](arkui-ts/ts-universal-attributes-attribute-modifier.md#AttributeModifier). You need to customize a class to inherit **AttributeUpdater**.
43
44**C** indicates the constructor type of the component, for example, **TextInterface** of the **Text** component and **ImageInterface** of the **Image** component. It is required only when **updateConstructorParams** is used.
45
46**System capability**: SystemCapability.ArkUI.ArkUI.Full
47
48### applyNormalAttribute
49applyNormalAttribute?(instance: T): void
50
51Defines the function for updating attributes in normal state.
52
53**Atomic service API**: This API can be used in atomic services since API version 12.
54
55**System capability**: SystemCapability.ArkUI.ArkUI.Full
56
57**Parameters**
58
59| Name | Type  | Mandatory | Description                                                                    |
60| ------ | ------ | ---- | ------------------------------------------------------------------------ |
61| instance | T | Yes | Component attribute class, which identifies the type of component to which attributes will be applied, for example, **ButtonAttribute** for the **Button** component and **TextAttribute** for the **Text** component.|
62
63### initializeModifier
64initializeModifier(instance: T): void
65
66Initialize the attributes to the values initially set by **AttributeUpdater** for the component.
67
68**Atomic service API**: This API can be used in atomic services since API version 12.
69
70**System capability**: SystemCapability.ArkUI.ArkUI.Full
71
72**Parameters**
73
74| Name | Type  | Mandatory | Description                                                                    |
75| ------ | ------ | ---- | ------------------------------------------------------------------------ |
76| instance | T | Yes | Component attribute class, which identifies the type of component to which attributes will be applied, for example, **ButtonAttribute** for the **Button** component and **TextAttribute** for the **Text** component.|
77
78**Example**
79
80This example shows how to use **initializeModifier** to initialize attribute values.
81
82```ts
83// xxx.ets
84import { AttributeUpdater } from '@kit.ArkUI'
85
86class MyButtonModifier extends AttributeUpdater<ButtonAttribute> {
87  initializeModifier(instance: ButtonAttribute): void {
88    instance.backgroundColor('#ff2787d9')
89      .width('50%')
90      .height(30)
91  }
92}
93
94@Entry
95@Component
96struct updaterDemo1 {
97  modifier: MyButtonModifier = new MyButtonModifier()
98
99  build() {
100    Row() {
101      Column() {
102        Button("Button")
103          .attributeModifier(this.modifier)
104      }
105      .width('100%')
106    }
107    .height('100%')
108  }
109}
110```
111![attributeUpdater1](figures/attribute-updater1.PNG)
112
113
114### attribute
115get attribute(): T | undefined
116
117Obtains the attribute class instance corresponding to the component in **AttributeUpdater**. The instance can then be used to directly update attributes.
118
119**Atomic service API**: This API can be used in atomic services since API version 12.
120
121**System capability**: SystemCapability.ArkUI.ArkUI.Full
122
123**Return value**
124
125| Type            | Description                                                        |
126| -------------------- | ------------------------------------------------------------ |
127| T \| undefined |Returns the attribute class instance of the component in **AttributeUpdater** if it exists; returns **undefined** otherwise.|
128
129**Example**
130
131This example shows how to directly update attributes through **AttributeUpdater**.
132
133```ts
134// xxx.ets
135import { AttributeUpdater } from '@kit.ArkUI'
136
137class MyButtonModifier extends AttributeUpdater<ButtonAttribute> {
138  initializeModifier(instance: ButtonAttribute): void {
139    instance.backgroundColor('#ffd5d5d5')
140      .width('50%')
141      .height(30)
142  }
143}
144
145@Entry
146@Component
147struct updaterDemo2 {
148  modifier: MyButtonModifier = new MyButtonModifier()
149
150  build() {
151    Row() {
152      Column() {
153        Button("Button")
154          .attributeModifier(this.modifier)
155          .onClick(() => {
156            this.modifier.attribute?.backgroundColor('#ff2787d9').width('30%')
157          })
158      }
159      .width('100%')
160    }
161    .height('100%')
162  }
163}
164```
165![attributeUpdater2](figures/attribute-updater2.gif)
166
167### updateConstructorParams
168updateConstructorParams: C
169
170Represents construction parameters used for updating component attributes.
171
172**C** indicates the constructor type of the component, for example, **TextInterface** of the **Text** component and **ImageInterface** of the **Image** component.
173
174**Atomic service API**: This API can be used in atomic services since API version 12.
175
176**System capability**: SystemCapability.ArkUI.ArkUI.Full
177
178**Example**
179
180This example demonstrates how to use **updateConstructorParams**.
181
182```ts
183// xxx.ets
184import { AttributeUpdater } from '@kit.ArkUI'
185
186class MyTextModifier extends AttributeUpdater<TextAttribute, TextInterface> {
187  initializeModifier(instance: TextAttribute) {
188  }
189}
190
191@Entry
192@Component
193struct attributeDemo3 {
194  private modifier: MyTextModifier = new MyTextModifier()
195
196  build() {
197    Row() {
198      Column() {
199        Text("Initialize")
200          .attributeModifier(this.modifier)
201          .fontSize(14).border({ width: 1 }).textAlign(TextAlign.Center).lineHeight(20)
202          .width(200).height(50)
203          .backgroundColor('#fff7f7f7')
204          .onClick(() => {
205            this.modifier.updateConstructorParams("Updated")
206          })
207      }
208      .width('100%')
209    }
210    .height('100%')
211  }
212}
213```
214![attributeUpdater3](figures/attribute-updater3.gif)
215
216### onComponentChanged
217
218onComponentChanged(instance: T): void
219
220Invoked to notify the application that the component bound to the same custom **Modifier** object changes.
221
222**Atomic service API**: This API can be used in atomic services since API version 12.
223
224**System capability**: SystemCapability.ArkUI.ArkUI.Full
225
226**Parameters**
227
228| Name | Type  | Mandatory | Description                                                                    |
229| ------ | ------ | ---- | ------------------------------------------------------------------------ |
230| instance | T | Yes | Component attribute class, which identifies the type of component to which attributes will be applied, for example, **ButtonAttribute** for the **Button** component and **TextAttribute** for the **Text** component.|
231
232**Example**
233
234```ts
235// xxx.ets
236import { AttributeUpdater } from '@kit.ArkUI'
237
238class MyButtonModifier extends AttributeUpdater<ButtonAttribute> {
239  initializeModifier(instance: ButtonAttribute): void {
240    instance.backgroundColor('#ff2787d9')
241      .width('50%')
242      .height(30)
243  }
244
245  onComponentChanged(instance: ButtonAttribute) :void {
246    instance.backgroundColor('#ff2787d9')
247      .width('50%')
248      .height(30)
249  }
250}
251
252@Entry
253@Component
254struct updaterDemo4 {
255  @State btnState: boolean = false
256  modifier: MyButtonModifier = new MyButtonModifier()
257
258  build() {
259    Row() {
260      Column() {
261        Button("Test")
262          .onClick(() => {
263          this.btnState = !this.btnState
264        })
265
266        if (this.btnState) {
267          Button("Button")
268            .attributeModifier(this.modifier)
269        } else {
270          Button("Button")
271            .attributeModifier(this.modifier)
272        }
273      }
274      .width('100%')
275    }
276    .height('100%')
277  }
278}
279