rules/arkts/patterns.md
This file extends common/patterns.md with HarmonyOS and ArkTS-specific patterns.
MUST use ArkUI State Management V2. V1 decorators are deprecated and must not be used.
| Decorator | Purpose |
|---|---|
@ComponentV2 | Marks a struct as a V2 component |
@Local | Local state within a component |
@Param | Props received from parent (read-only) |
@Event | Callback events from child to parent |
@Provider | Provides state to descendant components |
@Consumer | Consumes state from ancestor @Provider |
@Monitor | Watches for state changes (replaces V1 @Watch) |
@Computed | Derived/computed values |
@ObservedV2 | Makes a class observable for V2 state management |
@Trace | Marks observable properties in @ObservedV2 classes |
Never use: @State, @Prop, @Link, @ObjectLink, @Observed, @Provide, @Consume, @Watch, @Component (use @ComponentV2 instead).
@ObservedV2
class UserModel {
@Trace name: string = ''
@Trace age: number = 0
}
@ComponentV2
struct UserCard {
@Param user: UserModel = new UserModel()
@Event onDelete: () => void = () => {}
build() {
Column() {
Text(this.user.name)
.fontSize($r('app.float.font_size_title'))
Text(`${this.user.age}`)
.fontSize($r('app.float.font_size_body'))
Button($r('app.string.delete'))
.onClick(() => this.onDelete())
}
}
}
@ComponentV2
struct ParentPage {
@Provider('userState') userModel: UserModel = new UserModel()
build() {
Column() {
ChildComponent() // automatically receives @Consumer('userState')
}
}
}
@ComponentV2
struct ChildComponent {
@Consumer('userState') userModel: UserModel = new UserModel()
build() {
Text(this.userModel.name)
}
}
MUST use Navigation component with NavPathStack. Never use @ohos.router.
@ComponentV2
struct MainPage {
@Local navPathStack: NavPathStack = new NavPathStack()
build() {
Navigation(this.navPathStack) {
// Home content
}
.navDestination(this.routerMap)
}
@Builder
routerMap(name: string, param: ESObject) {
if (name === 'detail') {
DetailPage()
} else if (name === 'settings') {
SettingsPage()
}
}
}
// Push a new page
this.navPathStack.pushPath({ name: 'detail', param: { id: '123' } })
// Replace current page
this.navPathStack.replacePath({ name: 'settings' })
// Pop back
this.navPathStack.pop()
// Pop to root
this.navPathStack.clear()
@ComponentV2
struct DetailPage {
build() {
NavDestination() {
Column() {
Text($r('app.string.detail_title'))
}
}
.title($r('app.string.detail_nav_title'))
}
}
Recommended architecture for HarmonyOS applications:
feature/
|-- model/ # Data models (@ObservedV2 classes)
|-- viewmodel/ # Business logic (ViewModel classes)
|-- view/ # UI components (@ComponentV2 structs)
|-- service/ # API calls, data access
build()@ObservedV2 and @Trace@ComponentV2
struct AnimatedCard {
@Local isExpanded: boolean = false
@Local cardScale: number = 0.8
build() {
Column() {
// Content
}
.scale({ x: this.cardScale, y: this.cardScale })
.animation({ duration: 300, curve: Curve.EaseInOut })
.onClick(() => {
this.isExpanded = !this.isExpanded
this.cardScale = this.isExpanded ? 1.0 : 0.8
})
}
}
renderGroup(true) for complex sub-component animations to reduce render batcheswidth, height, padding, margin during animations - severe performance impactanimateTo for explicit animation controltransform (translate, scale, rotate) and opacity for performant animations@ComponentV2
struct LargeList {
@Local dataSource: MyDataSource = new MyDataSource()
build() {
List() {
LazyForEach(this.dataSource, (item: ItemModel) => {
ListItem() {
ItemComponent({ item: item })
}
}, (item: ItemModel) => item.id)
}
}
}
@Builder for lightweight UI fragments within a component@Param for configurable componentsAlways define UI constants as resources and reference via $r():
// BAD: hardcoded values
Text('Hello')
.fontSize(16)
.fontColor('#333333')
// GOOD: resource references
Text($r('app.string.greeting'))
.fontSize($r('app.float.font_size_body'))
.fontColor($r('app.color.text_primary'))