1# Basic UI Description 2 3In ArkTS, you define a custom component by using decorators **@Component** and **@Entry** to decorate a data structure declared with the **struct** keyword. A custom component provides a **build** function, where you must write the basic UI description in chain call mode. For details about the UI description, see [UI Description Specifications](#ui-description-specifications). 4 5## Basic Concepts 6 7- struct: a data structure that can be used to implement custom components and cannot have inheritance. The **new** keyword can be omitted when initializing a struct. 8 9- Decorator: a special type of declaration that can be applied to classes, structures, or class attributes to add new functionality to them. Multiple decorators can be applied to the same target element and defined on a single line or multiple lines. It is recommended that the decorators be defined on multiple lines. 10 11 ```ts 12 @Entry 13 @Component 14 struct MyComponent { 15 } 16 ``` 17 18- **build** function: A custom component must implement the **build** function and must implement no constructor. The **build** function meets the definition of the **Builder** API and is used to define the declarative UI description of components. 19 20 ```ts 21 interface Builder { 22 build: () => void 23 } 24 ``` 25 26- **@Component**: a decorator applied to a struct to equip it with the component-based capability. The **build** method must be implemented for UI creation. 27 28- **@Entry**: a decorator applied to a struct to make it the entry to a page, which is rendered and displayed when the page is loaded. 29 30- **@Preview**: a decorator applied to struct to make it previewable in the DevEco Studio Previewer. The decorated component is created and displayed when the residing page is loaded. 31 32 > **NOTE** 33 > 34 > In a single source file, you can use up to 10 **@Preview** decorators to decorate custom components. For details, see [Previewing ArkTS Components](https://developer.harmonyos.com/en/docs/documentation/doc-guides/ohos-previewing-app-service-0000001218760596#section146052489820). 35 36- Chain call: a syntax for configuring the attribute methods, event methods, and more of UI components by using the dot notation. 37 38## UI Description Specifications 39 40### Structs Without Parameters 41 42A struct without parameters is a component whose API definition has empty parentheses. No parameter needs to be passed to this type of component, for example, the **Divider** component in the following snippet: 43 44```ts 45Column() { 46 Text('item 1') 47 Divider() 48 Text('item 2') 49} 50``` 51 52### Structs with Parameters 53 54A struct with parameters is a component whose API definition expects parameters enclosed in the parentheses. You can use constants to assign values to the parameters. 55 56Sample code: 57 58- Set the mandatory parameter **src** of the **\<Image>** component as follows: 59 60 ```ts 61 Image('https://xyz/test.jpg') 62 ``` 63 64- Set the optional parameter **content** of the **\<Text>** component as follows: 65 66 ```ts 67 Text('test') 68 ``` 69 70You can use variables or expressions to assign values to parameters. The result type returned by an expression must meet the parameter type requirements. For details about the variables, see [State Management with Page-level Variables](arkts-state-mgmt-page-level.md) and [State Management with Application-level Variables](arkts-state-mgmt-application-level.md). For example, set a variable or expression to construct the **\<Image>** and **\<Text>** components: 71 72```ts 73Image(this.imagePath) 74Image('https://' + this.imageUrl) 75Text(`count: ${this.count}`) 76``` 77 78### Attribute Configuration 79 80Component attributes are configured using an attribute method, which follows the corresponding component and is bound to the component using the "**.**" operator. 81 82- Example of configuring the font size attribute of the **\<Text>** component: 83 84 ```ts 85 Text('test') 86 .fontSize(12) 87 ``` 88 89- Example of configuring multiple attributes at the same time by using the "**.**" operator to implement chain call: 90 91 ```ts 92 Image('test.jpg') 93 .alt('error.jpg') 94 .width(100) 95 .height(100) 96 ``` 97 98- Example of passing variables or expressions in addition to constants: 99 100 ```ts 101 Text('hello') 102 .fontSize(this.size) 103 Image('test.jpg') 104 .width(this.count % 2 === 0 ? 100 : 200) 105 .height(this.offset + 100) 106 ``` 107 108- For attributes of built-in components, ArkUI also provides some predefined [enumeration types](../reference/arkui-ts/ts-appendix-enums.md), which you can pass as parameters to methods if they meet the parameter type requirements. For example, you can configure the font color and weight attributes of the **\<Text>** component as follows: 109 110 ```ts 111 Text('hello') 112 .fontSize(20) 113 .fontColor(Color.Red) 114 .fontWeight(FontWeight.Bold) 115 ``` 116 117### Event Configuration 118 119Events supported by components are configured using event methods, which each follow the corresponding component and are bound to the component using the "**.**" operator. 120 121- Example of using a lambda expression to configure the event of a component: 122 123 ```ts 124 Button('add counter') 125 .onClick(() => { 126 this.counter += 2; 127 }) 128 ``` 129 130- Example of using an anonymous function expression to configure the event of a component (**bind** must be used to ensure that the contained components are referenced by **this** in the function body): 131 132 ```ts 133 Button('add counter') 134 .onClick(function () { 135 this.counter += 2; 136 }.bind(this)) 137 ``` 138 139- Example of using a component's member function to configure the event of the component: 140 141 ```ts 142 myClickHandler(): void { 143 this.counter += 2; 144 } 145 146 ... 147 148 Button('add counter') 149 .onClick(this.myClickHandler.bind(this)) 150 ``` 151 152### Child Component Configuration 153 154For a component that supports child components, for example, a container component, add the UI descriptions of the child components inside parentheses. The **\<Column>**, **\<Row>**, **\<Stack>**, **\<Grid>**, and **\<List>** components are all container components. 155 156- Simple example of the **\<Column>** component: 157 158 ```ts 159 Column() { 160 Text('Hello') 161 .fontSize(100) 162 Divider() 163 Text(this.myText) 164 .fontSize(100) 165 .fontColor(Color.Red) 166 } 167 ``` 168 169- Example of nesting multiple child components in the **\<Column>** component: 170 171 ```ts 172 Column() { 173 Row() { 174 Image('test1.jpg') 175 .width(100) 176 .height(100) 177 Button('click +1') 178 .onClick(() => { 179 console.info('+1 clicked!'); 180 }) 181 } 182 183 Divider() 184 Row() { 185 Image('test2.jpg') 186 .width(100) 187 .height(100) 188 Button('click +2') 189 .onClick(() => { 190 console.info('+2 clicked!'); 191 }) 192 } 193 194 Divider() 195 Row() { 196 Image('test3.jpg') 197 .width(100) 198 .height(100) 199 Button('click +3') 200 .onClick(() => { 201 console.info('+3 clicked!'); 202 }) 203 } 204 } 205 ``` 206