This content originally appeared on DEV Community and was authored by kouwei qing
[Daily HarmonyOS Next Knowledge] Lifecycle Triggering, This Pointer, Countdown Component, State Variables, Swiper Component
1. HarmonyOS lifecycle not triggering?
The lifecycle of the page corresponding to a tab does not trigger or triggers only once.
Use the onTabBarClick
event to trigger actions when tabs are clicked. Reference: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-components-container-tabs-V5
2. Issue with this
pointer in HarmonyOS custom callbacks; unable to call methods within the Page?
Save the this
pointer outside the method:
let _this = this
onReceiveData(){
......
}
3. HarmonyOS TextTimer component shows 0 first when isCountDown
is true
, then starts counting down?
Code example:
TextTimer({ isCountDown: false, count: 60000, controller: this.textTimerController })
.size({ width: 100, height: '100%' })
.margin({ left: 12 })
.format('s')
.fontColor(Color.White)
.fontSize(14)
.align(Alignment.Center)
.onTimer((utc: number, elapsedTime: number) => {
console.info('textTimer notCountDown utc is:' + utc + ', elapsedTime: ' + elapsedTime)
})
In the demo, a 60-second countdown automatically converts to 1 minute (01:00.00). Using format('s')
will display only seconds (0). There is no configuration to disable this automatic conversion. Alternatively, implement the countdown manually:
@Entry
@Component
struct TestTextTimerPage {
@State timeTaskNumber: number = 120;
task: number = -1;
countDown() {
this.task = setInterval(() => {
this.timeTaskNumber--;
if (this.timeTaskNumber == 0) {
clearInterval(this.task);
this.task = -1;
}
}, 1000);
}
build() {
Column() {
Row() {
Text(this.timeTaskNumber + '');
Button('start').onClick(() => {
this.countDown();
});
Button('stop').onClick(() => {
if (this.task >= 0) {
clearInterval(this.task);
this.task = -1;
}
})
}
}
}
}
4. Parameters in HarmonyOS Builder functions for initializing @Provide variables in Components do not update when Builder parameters change?
Variables decorated with @Provide and @prop should both be stateful, but there are differences. When using the BuilderNode's update
function to update uiFunc parameters, only @Prop-decorated args2
updates, while @Provide-decorated args1
does not.
@Provide on a parent component is equivalent to @State. update
passes data from the builderNode to child components. Custom components are children of the builderNode; @prop properties update from parent to child, while @Provide updates from ancestor to descendant. Refer to state management documentation:
- https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/quick-start/Readme-CN.md
- https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/quick-start/arkts-provide-and-consume.md
@Provide and @Consume enable two-way data synchronization between ancestors and descendants, supporting cross-level state propagation. Unlike parameter passing between parent and child components, @Provide and @Consume allow direct cross-level binding:
- @Provide: Decorates variables in ancestor components to "provide" state to descendants.
- @Consume: Decorates variables in descendant components to "consume" (bind to) ancestor-provided state.
Before using @Provide/@Consume, developers should understand basic UI syntax and custom components.
5. HarmonyOS Swiper's displayCount
set to auto
does not adapt when items use the position
attribute?
position
uses absolute positioning, fixing child component positions relative to the parent. Reference: https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/arkui-ts/ts-universal-attributes-location.md
This content originally appeared on DEV Community and was authored by kouwei qing

kouwei qing | Sciencx (2025-06-28T12:14:52+00:00) Lifecycle Triggering, This Pointer, Countdown Component, State Variables, Swiper Component. Retrieved from https://www.scien.cx/2025/06/28/lifecycle-triggering-this-pointer-countdown-component-state-variables-swiper-component/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.