Can the lifecycle function of React be preceded by async?

look at some code that is written like this and can still run, life cycle hook plus async, does not quite understand this behavior, not to say that componentWillMount is Synchronize, execute in front of render, so it will not block?

class Edit extends Component{
    constructor(){
    }
    async componentWillMount(){
        await someMethod();
    }
    render(){
    }
}
Jun.21,2021

adding async can only guarantee Synchronize inside the function

function getData(){
    new Promise((resolve)=>{
        setTimeout(()=>{
            resolve()
        },2000)
    })
}

async function foo(){
  console.log(1)
  await getData()//2
  console.log(2)
}

foo().then(()=>{})
console.log(3)

// 1 3
// 
// 2

foo external or a normal Promise function only ensures that its interior is Synchronize


of course, you can add it. After adding async/await, the asynchronous operation in the lifecycle function is Synchronize, but it does not affect the lifecycle itself. For example, your code still does not execute render () until componentWillMount () is finished.

add: asynchronous component loading is realized by using async/await, and dynamic import

Menu