The setData function is used to send data from the logical layer to the view layer (asynchronously) and change the corresponding value of this.data (synchronously).

When the data changes, the logical layer’s this.data is changed, but the rendering layer’s presentation of the page is not changed, because it is asynchronous

The test code

Page({

    data: {
        testData: 0
    },
    
    onLoad() {
        console.log('start'.this.data.testData)
    
        this.setData({
            testData: 1= {}, ()console.log('Callback execution')})console.log('end'.this.data.testData)
        
        // Simulate js long time operation
        for(let i=0; i<1000000000000000000000000; i++) {}
        
        console.log('Long time'.this.data.testData)
    }

})
Copy the code

Print the result

start 0
end 1Long time1The callback executionCopy the code

Change the value of a property of an object in data

Page({
  data: {
    array: [{text: 'init data'}].object: {
      name: 'name'.age: 1}},onLoad(){
       // * Error: the name attribute will be overwritten
       this.setData({
          object: {
              age: 2}})/ / right
       this.setData({
          'array[0].text':'changed data'.'object.age': 2})}})Copy the code