Vue與React的異同 -生命週期

vue的生命週期

建立前 beforeCreatevue

建立   createreact

掛載前 beforeMountajax

掛載 mounted服務器

更新前 beforeUpdateapp

更新 updatedthis

銷燬前 beforeDestroyedspa

銷燬  destoryedcomponent

 methods --方法orm

Vue 实例生命周期

-----------------------------------華麗的分割線-------------------------生命週期

一個React組件的生命週期分爲三個部分:掛載期(Mounting)、存在更新期(Updating)和銷燬時(Unmounting)。

import React,{ Component } from 'react';

class Demo extends Component {
constructor(props,context) {
super(props,context)
this.state = {
//定義state
}
}
componentWillMount () { // 組件將要掛載
}
componentDidMount () { // 組件渲染完成 這裏調用ajax請求,返回數據setstate組件會從新渲染
}
componentWillReceiveProps (nextProps) {
}
shouldComponentUpdate (nextProps,nextState) {
}
componentWillUpdate (nextProps,nextState) {
}
componentDidUpdate (prevProps,prevState) {
}
render () {
return (
<div></div>
)
}
componentWillUnmount () {
}
}
export default Demo;

 

componentDidMount方法中放ajax請求--服務器會根據變量渲染, Constructor中設置變量初始化


 

若是你以爲個人文章對您有幫助,給點鼓勵,謝謝

 

相關文章
相關標籤/搜索