React 生命周期函数

it2022-05-05  139

在组件即将被挂载到页面的时刻自动执行,只有在第一次挂载的时候执行

componentWillMount(){ console.log('componentWillMount') }

组件被挂载到页面之后,自动被执行,只有在第一次挂载的时候执行

import axios from 'axios';//引入axios请求 componentDidMount(){ //一般在次钩子函数进行axios请求,npm install -save axios 安装数据请求 console.log('componentDidMount') axios.get('https://www.xxxx') .then(res=>{ console.log(res) }) }

组件被更新之前,会自动执行

shouldComponentUpdate(){ console.log('shouldComponentUpdate') //必须要返回一个布尔值 return true }

组件被更新之前会自动执行,但是他是在shouldComponentUpdate之后执行 如果shouldComponentUpdate放回true它才执行 如果shouldComponentUpdate放回false它就不执行

componentWillUpdate(){ console.log('componentWillUpdate') }

组件更新完成之后它会执行

componentDidUpdate(){ console.log('componentDidUpdate') }

当一个组件从父组件接受参数 只要父组件的render函数被重新执行了,子组件的这个生命周期函数就会被执行 如果这个组件第一次存在于父组件中,就不会执行 如果这个组件之前就已经存在于父组件中,才会执行

componentWillReceiveProps(){ console.log('componentWillReceiveProps') }

当这个组件即将被从页面中剔除的时候,就会执行

componentWillUnmount(){ console.log('componentWillUnmount') }

最新回复(0)