All Products
Search
Document Center

Mobile Platform as a Service:Component lifecycle

Last Updated:Feb 04, 2021

The life cycle function of a component is triggered by the framework at a special time point . The following diagram shows the life cycle of a component.

Detailed information of the life cycle function is as follows.

Life cycle Field Description Minimum version
onInit N/A Triggered when the component creation starts 1.14.0
deriveDataFromProps nextProps Triggered when the component creation starts and before the component is updated 1.14.0
didMount N/A Triggered when the component creation completes -
didUpdate (prevProps,prevData) Triggered when the component is updated -
didUnmount N/A Triggered when the component is deleted -
Note: 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:

  • Access the this.is, this.$id, and this.$page properties.
  • Access the this.data and this.props properties.
  • Access the custom properties of the methods component.
  • Call this.setData and this.$spliceData to modify data.

Code sample 1

  1. // /components/counter/index.js
  2. Component({
  3. data: {
  4. counter: 0,
  5. },
  6. onInit() {
  7. this.setData({
  8. counter: 1,
  9. is: this.is,
  10. });
  11. },
  12. })
  1. <!-- /components/counter/index.axml -->
  2. <view>{{counter}}</view>
  3. <view>{{is}}</view>

After the component is rendered on the page, the following output is displayed.

  1. 1
  2. /components/counter/index

Code sample 2

  1. // /components/counter/index.js
  2. Component({
  3. onInit() {
  4. this.xxx = 2;
  5. this.data = { counter: 0 };
  6. },
  7. })
  1. <!-- /components/counter/index.axml -->
  2. <view>{{counter}}</view>

After the component is rendered on the page, the following output is displayed.

  1. 0

deriveDataFromProps

deriveDataFromProps is triggered when the component is created and updated. In deriveDataFromProps, you can:

  • Access the this.is, this.$id, and this.$page properties.
  • Access the this.data and this.props properties.
  • Access the custom properties of the methods component.
  • Call this.setData and this.$spliceData to modify data.
  • Use nextProps to obtain the props parameter to be modified.

Code sample

  1. // /components/counter/index.js
  2. Component({
  3. data: {
  4. counter: 5,
  5. },
  6. deriveDataFromProps(nextProps) {
  7. if (this.data.counter < nextProps.pCounter) {
  8. this.setData({
  9. counter: nextProps.pCounter,
  10. });
  11. }
  12. },
  13. })
  1. <!-- /components/counter/index.axml -->
  2. <view>{{counter}}</view>
  1. // /pages/index/index.js
  2. Page({
  3. data: {
  4. counter: 1,
  5. },
  6. plus() {
  7. this.setData({ counter: this.data.counter + 1 })
  8. },
  9. })
  1. <!-- /pages/index/index.axml -->
  2. <counter pCounter="{{counter}}" />
  3. <button onTap="plus">+</button>
Note: 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 the callback after the custom component is rendered for the first time. At this time, the page has been rendered, and the server data is usually requested at this time.

Code sample

  1. Component({
  2. data: {},
  3. didMount() {
  4. let that = this;
  5. my.httpRequest({
  6. url: 'http://httpbin.org/post',
  7. success: function(res) {
  8. console.log(res);
  9. that.setData({name: 'xiaoming'});
  10. }
  11. });
  12. },
  13. });

didUpdate

didUpdate is the callback after the data of the custom component is updated. It will be called every time the data of the component changes.

Code sample

  1. Component({
  2. data: {},
  3. didUpdate(prevProps, prevData) {
  4. console.log(prevProps, this.props, prevData, this.data);
  5. },
  6. });
Notes:
  • didUpdate is triggered when this.setData is called in the component.
  • didUpdate can also be triggered when this.setData is called by an external caller.

didUnmount

didUnmount is the callback after the custom component is deleted. It is triggered every time the component instance is deleted from the page.

Code sample

  1. Component({
  2. data: {},
  3. didUnmount() {
  4. console.log(this);
  5. },
  6. });