ts-component-creation-re-initialization.md 3.6 KB
Newer Older
G
ge-yafang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
# Component Creation and Re-initialization


## Initial Creation and Rendering

1. Create the parent component ParentComp.

2. Locally initialize the state variable isCountDown of ParentComp.

3. Execute the build function of ParentComp.

4. Create a preset <Column> component.
   1. Create a preset <Text> component, set the text content to be displayed, and add the <Text> component instance to the <Column> component.
   2. Create the component on the true branch based on the if condition.
       1. Create a preset <Image> component and set the image source address.
       2. Create a TimerComponent using the given constructor.
           1. Create a TimerComponent object.
           2. Initialize the values of member variables locally.
           3. Use the parameters provided by the TimerComponent constructor to update the values of member variables.
           4. Execute the aboutToAppear function of TimerComponent.
           5. Execute the build function of TimerComponent to create the corresponding UI description structure.
   3. Create a preset <Button> component and set the corresponding content.


## Status Update

When a user clicks a button:

1. The value of the isCountDown state variable of ParentComp is changed to false.

2. The build function of ParentComp is executed.

3. The preset <Column> component is reused by the framework and reinitialized.

4. The child components of <Column> reuse and reinitialize the objects in the memory.
   1. Reuse the preset <Text> component after re-initializing the component using new text content.
   2. Reuse the component on the false branch based on the if condition.
       1. Destroy the components on the original true branch as these components are no longer used.
           1. Destroy the created preset <image> component instance.
           2. Destroy the TimerComponent component instance, and call the aboutToDisappear function.
       2. Create components on the false branch.
           1. Create a preset <Image> component and set the image source address.
           2. Create a TimerComponent again using the given constructor.
           3. Initialize the newly created TimerComponent and call the aboutToAppear and build functions.
   3. Reuse the preset <Button> component, with the new image source address.


## Example

Z
zengyawen 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

```
@Entry
@Component
struct ParentComp {
    @State isCountDown: boolean = true
    build() {
        Column() {
            Text(this.isCountDown ? 'Count Down' : 'Stopwatch')
            if (this.isCountDown) {
                Image('countdown.png')
                TimerComponent({counter: 10, changePerSec: -1, showInColor: Color.Red})
            } else {
                Image('stopwatch.png')
                TimerComponent({counter: 0, changePerSec: +1, showInColor: Color.Black })
            }
            Button(this.isCountDown ? 'Swtich to Stopwatch' : 'Switch to Count Down')
G
ge-yafang 已提交
67
                .onClick(() => {this.isCountDown = !this.isCountDown})
Z
zengyawen 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        }
    }
}

// Manage and display a count down / stop watch timer
@Component
struct TimerComponent {
    @State counter: number = 0
    private changePerSec: number = -1
    private showInColor: Color = Color.Black
    private timerId : number = -1

    build() {
        Text(`${this.counter}sec`)
            .fontColor(this.showInColor)
    }

    aboutToAppear() {
G
ge-yafang 已提交
86
        this.timerId = setInterval(() => {this.counter += this.changePerSec}, 1000)
Z
zengyawen 已提交
87 88 89
    }

    aboutToDisappear() {
G
ge-yafang 已提交
90
        if (this.timerId > 0) {
Z
zengyawen 已提交
91 92 93 94 95 96
            clearTimeout(this.timerId)
            this.timerId = -1
        }
    }
}   
```