1# Custom Component Lifecycle 2 3The lifecycle callbacks of a custom component are used to notify users of the lifecycle of the component. These callbacks are private and are invoked by the development framework at a specified time at runtime. They cannot be manually invoked from applications. 4 5>**NOTE** 6> 7>- The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8>- Promise and asynchronous callback functions can be used in lifecycle functions, for example, network resource getters and timer setters. 9 10 11## aboutToAppear 12 13aboutToAppear?(): void 14 15Invoked after a new instance of the custom component is created and before its **build** function is executed. You can change state variables in the **aboutToAppear** function. The change will take effect when you execute the **build** function next time. 16 17Since API version 9, this API is supported in ArkTS widgets. 18 19## aboutToDisappear 20 21aboutToDisappear?(): void 22 23Invoked before the destructor of the custom component is consumed. Do not change state variables in the **aboutToDisappear** function as doing this can cause unexpected errors. For example, the modification of the **@Link** decorated variable may cause unstable application running. 24 25Since API version 9, this API is supported in ArkTS widgets. 26 27## onPageShow 28 29onPageShow?(): void 30 31Invoked when a page is displayed. This callback is used in the routing process or scenarios where the application is switched to the foreground or background. It works only for the custom components decorated by **@Entry**. 32 33 34## onPageHide 35 36onPageHide?(): void 37 38Invoked when a page is hidden. This callback is used in the routing process or scenarios where the application is switched to the foreground or background. It works only for the custom components decorated by **@Entry**. 39 40 41## onBackPress 42 43onBackPress?(): void | boolean 44 45Invoked when the user clicks the Back button. It works only for the custom components decorated by @Entry. The value **true** means that the page executes its own return logic instead of the , and **false** (default) means that the default return logic is used. 46 47 48```ts 49// xxx.ets 50@Entry 51@Component 52struct IndexComponent { 53 @State textColor: Color = Color.Black; 54 55 onPageShow() { 56 this.textColor = Color.Blue; 57 console.info('IndexComponent onPageShow'); 58 } 59 60 onPageHide() { 61 this.textColor = Color.Transparent; 62 console.info('IndexComponent onPageHide'); 63 } 64 65 onBackPress() { 66 this.textColor = Color.Red; 67 console.info('IndexComponent onBackPress'); 68 } 69 70 build() { 71 Column() { 72 Text('Hello World') 73 .fontColor(this.textColor) 74 .fontSize(30) 75 .margin(30) 76 }.width('100%') 77 } 78} 79``` 80 81 82## onLayout<sup>9+</sup> 83 84onLayout?(children: Array<LayoutChild>, constraint: ConstraintSizeOptions): void 85 86Invoked when the custom component lays out its child components. Through this callback the component receives its child component layout information and size constraint from the framework. The state variable cannot be changed in the **onLayout** callback. 87 88Since API version 9, this API is supported in ArkTS widgets. 89 90**Parameters** 91 92| Name | Type | Description | 93| ---------- | ---------------------------------------- | ---------------- | 94| children | Array<[LayoutChild](#layoutchild9)> | Child component layout information. | 95| constraint | [ConstraintSizeOptions](ts-types.md#constraintsizeoptions) | Size constraint information of the parent component.| 96 97 98## onMeasure<sup>9+</sup> 99 100onMeasure?(children: Array<LayoutChild>, constraint: ConstraintSizeOptions): void 101 102Invoked when the custom component lays out its child components. Through this callback the component receives its child component layout information and size constraint from the framework. The state variable cannot be changed in the **onMeasure** callback. 103 104Since API version 9, this API is supported in ArkTS widgets. 105 106**Parameters** 107 108| Name | Type | Description | 109| ---------- | ---------------------------------------- | ---------------- | 110| children | Array<[LayoutChild](#layoutchild9)> | Child component layout information. | 111| constraint | [ConstraintSizeOptions](ts-types.md#constraintsizeoptions) | Size constraint information of the parent component.| 112 113## aboutToReuse<sup>10+</sup> 114 115aboutToReuse?(params: { [key: string]: unknown }): void 116 117Invoked when a custom component previously placed in the cache for future reuse is re-added to the node tree, with the parameters used for constructing the component passed in. 118 119Since API version 10, this API is supported in ArkTS widgets. 120 121**Parameters** 122 123| Name | Type | Description | 124| ------ | -------------------------- | ---------- | 125| params | { [key: string]: unknown } | Parameters used for constructing the custom component.| 126 127 128```ts 129// xxx.ets 130export class Message { 131 value: string | undefined; 132 133 constructor(value: string) { 134 this.value = value 135 } 136} 137 138@Entry 139@Component 140struct Index { 141 @State switch: boolean = true 142 143 build() { 144 Column() { 145 Button('Hello World') 146 .fontSize(50) 147 .fontWeight(FontWeight.Bold) 148 .onClick(() => { 149 this.switch = !this.switch 150 }) 151 if (this.switch) { 152 Child({ message: new Message('Child') }) 153 } 154 } 155 .height("100%") 156 .width('100%') 157 } 158} 159 160@Reusable 161@Component 162struct Child { 163 @State message: Message = new Message('AboutToReuse'); 164 165 aboutToReuse(params: Record<string, ESObject>) { 166 console.info("Recycle Child") 167 this.message = params.message as Message 168 } 169 170 build() { 171 Column() { 172 Text(this.message.value) 173 .fontSize(20) 174 } 175 .borderWidth(2) 176 .height(100) 177 } 178} 179``` 180 181## LayoutChild<sup>9+</sup> 182 183Provides the child component layout information. 184 185Since API version 9, this API is supported in ArkTS widgets. 186 187| Parameter | Type | Description | 188| ---------- | ---------------------------------------- | ------------------- | 189| name | string | Name of the child component. | 190| id | string | ID of the child component. | 191| constraint | [ConstraintSizeOptions](ts-types.md#constraintsizeoptions) | Constraint size of the child component. | 192| borderInfo | [LayoutBorderInfo](#layoutborderinfo9) | Provides the border information of the child component. | 193| position | [Position](ts-types.md#position8) | Position coordinates of the child component. | 194| measure | (childConstraint:) => void | Method called to apply the size constraint to the child component.| 195| layout | (LayoutInfo: [LayoutInfo](#layoutinfo9)) => void| Method called to apply the layout information to the child component.| 196 197 198## LayoutBorderInfo<sup>9+</sup> 199 200Provides the border information of the child component. 201 202Since API version 9, this API is supported in ArkTS widgets. 203 204| Parameter | Type | Description | 205| ----------- | ------------------------------------ | ----------------------- | 206| borderWidth | [EdgeWidths](ts-types.md#edgewidths9) | Edge widths in different directions of the component.| 207| margin | [Margin](ts-types.md#margin) | Margins in different directions of the component. | 208| padding | [Padding](ts-types.md#padding) | Paddings in different directions of the component. | 209 210 211## LayoutInfo<sup>9+</sup> 212 213Provides the layout information of the child component. 214 215Since API version 9, this API is supported in ArkTS widgets. 216 217| Parameter | Type | Description | 218| ---------- | ---------------------------------------- | -------- | 219| position | [Position](ts-types.md#position8) | Position coordinates of the child component.| 220| constraint | [ConstraintSizeOptions](ts-types.md#constraintsizeoptions) | Constraint size of the child component.| 221 222 223```ts 224// xxx.ets 225@Entry 226@Component 227struct Index { 228 build() { 229 Column() { 230 CustomLayout() { 231 ForEach([1, 2, 3], (index: number) => { 232 Text('Sub' + index) 233 .fontSize(30) 234 .borderWidth(2) 235 }) 236 } 237 } 238 } 239} 240 241 242@Component 243struct CustomLayout { 244 @Builder 245 doNothingBuilder() { 246 }; 247 248 @BuilderParam builder: () => void = this.doNothingBuilder; 249 250 onLayout(children: Array<LayoutChild>, constraint: ConstraintSizeOptions) { 251 let pos = 0; 252 children.forEach((child) => { 253 child.layout({ position: { x: pos, y: pos }, constraint: constraint }) 254 pos += 70; 255 }) 256 } 257 258 onMeasure(children: Array<LayoutChild>, constraint: ConstraintSizeOptions) { 259 let size = 100; 260 children.forEach((child) => { 261 child.measure({ minHeight: size, minWidth: size, maxWidth: size, maxHeight: size }) 262 size += 50; 263 }) 264 } 265 266 build() { 267 this.builder() 268 } 269} 270``` 271 272 273