The framework triggers component lifecycle functions at specific points during a component's existence. The following diagram illustrates the component lifecycle.

The following table describes each lifecycle function.
|
Life cycle |
Field |
Description |
Minimum version |
|
onInit |
N/A |
Triggered when component creation starts. |
|
|
deriveDataFromProps |
nextProps |
Triggered when component creation starts and before the component updates. |
|
|
didMount |
N/A |
Triggered when component creation completes. |
- |
|
didUpdate |
(prevProps,prevData) |
Triggered when the component updates. |
- |
|
didUnmount |
N/A |
Triggered when the component is removed. |
- |
onInit and deriveDataFromProps apply to basic library 1.14.0 and later versions. You can use my.canIUse('component2') for compatibility.
onInit
onInit is triggered when a component is created. In onInit, you can perform the following operations:
-
Access the
this.is,this.$id, andthis.$pageproperties. -
Access the
this.dataandthis.propsproperties. -
Access custom properties defined in the component's methods.
-
Call
this.setDataandthis.$spliceDatato modify data.
Code sample 1
// /components/counter/index.js
Component({
data: {
counter: 0,
},
onInit() {
this.setData({
counter: 1,
is: this.is,
});
},
})<!-- /components/counter/index.axml -->
<view>{{counter}}</view>
<view>{{is}}</view>
After the component is rendered on the page, the output is as follows:
1
/components/counter/index
Code sample 2
// /components/counter/index.js
Component({
onInit() {
this.xxx = 2;
this.data = { counter: 0 };
},
})<!-- /components/counter/index.axml -->
<view>{{counter}}</view>
After the component is rendered on the page, the output is as follows:
0
deriveDataFromProps
deriveDataFromProps is triggered when the component is created and updated. In deriveDataFromProps, you can perform the following operations:
-
Access the
this.is,this.$id, andthis.$pageproperties. -
Access the
this.dataandthis.propsproperties. -
Access custom properties defined in the component's methods.
-
Call
this.setDataandthis.$spliceDatato modify data. -
Use
nextPropsto get the upcomingpropsvalues.
Code sample
// /components/counter/index.js
Component({
data: {
counter: 5,
},
deriveDataFromProps(nextProps) {
if (this.data.counter < nextProps.pCounter) {
this.setData({
counter: nextProps.pCounter,
});
}
},
})<!-- /components/counter/index.axml -->
<view>{{counter}}</view>// /pages/index/index.js
Page({
data: {
counter: 1,
},
plus() {
this.setData({ counter: this.data.counter + 1 })
},
})<!-- /pages/index/index.axml -->
<counter pCounter="{{counter}}" />
<button onTap="plus">+</button>
In this example, after you tap the + button, the counter on the page remains unchanged until the value of pCounter is greater than 5.
didMount
didMount is called after the custom component is rendered for the first time. At this point, the page is fully rendered. You typically request server data in this callback.
Code sample
Component({
data: {},
didMount() {
let that = this;
my.httpRequest({
url: 'http://httpbin.org/post',
success: function(res) {
console.log(res);
that.setData({name: 'xiaoming'});
}
});
},
});
didUpdate
didUpdate is called after the custom component data updates. It runs every time the component data changes.
Code sample
Component({
data: {},
didUpdate(prevProps, prevData) {
console.log(prevProps, this.props, prevData, this.data);
},
});
-
Calling
this.setDatainside a component triggers didUpdate. -
Calling
this.setDatafrom an external caller also triggers didUpdate.
didUnmount
didUnmount is called after the custom component is removed. It runs every time the component instance is removed from the page.
Code sample
Component({
data: {},
didUnmount() {
console.log(this);
},
});