Vue與React的異同 -生命周期
阿新 • • 發佈:2018-11-07
dem str destroy contex alt recreate width from style 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組件會重新渲染}componentWillReceiveProps (nextProps) {}shouldComponentUpdate (nextProps,nextState) {
在componentDidMount方法中放ajax請求--服務器會根據變量渲染,Constructor中設置變量初始化
如果你覺得我的文章對您有幫助,給點鼓勵,謝謝
Vue與React的異同 -生命周期