The code snippet is as follows:
@Component
struct ComponentA {
aboutToAppear(): void {
// Perception components are visible and hidden
console.log('Component A display');
}
aboutToDisappear(): void {
// Perception components are visible and hidden
console.log('Component A hidden');
}
build() {
Column() {
Text('Component A').fontSize(16).fontColor(Color.Black);
}
.width(100)
.height(50)
}
}
@Entry
@Component
struct ComponentB {
@State @Watch('onCompAShowStatusChange') isShowA: boolean = false;
onCompAShowStatusChange() {
// Perception components are visible and hidden
console.log('Monitor component A:' + `${this.isShowA ? 'display' : 'hide'}`);
}
build() {
Column() {
Button('Switch between visible and hidden').type(ButtonType.Normal).width(100).height(50).onClick(() => {
this.isShowA = !this.isShowA;
})
if (this.isShowA) {
ComponentA();
}
}
}
} Reference
@Watch Decorator: Getting Notified of State Variable Changes, if/else: Conditional Rendering