Vue與React的異同 -生命週期
阿新 • • 發佈:2018-11-07
vue的生命週期
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;
建立前 beforeCreate
建立 create
掛載前 beforeMount
掛載 mounted
更新前 beforeUpdate
更新 updated
銷燬前 beforeDestroyed
銷燬 destoryed
methods --方法
-----------------------------------華麗的分割線-------------------------
一個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元件會重新渲染
在 componentDidMount方法中放ajax請求--伺服器會根據變數渲染, Constructor中設定變數初始化
如果你覺得我的文章對您有幫助,給點鼓勵,謝謝