zoukankan      html  css  js  c++  java
  • react——生命周期函数

    import React from 'react'
    class LifeCycle extends React.Component{
        constructor(props) {
            console.log('我是constructor')
            super(props)
            this.state = {
                value: '生命周期组件'
            }
        }
        componentWillMount() { // 将要挂载
            console.log('我是componentWillMount')
        }
        componentDidMount() { // 已经挂载
            console.log('我是componentDidMount')
        }
        componentWillUpdate(nextProps, nextState, nextContext) { // 数据将要更新前
            console.log('我是componentWillUpdate')
        }
        shouldComponentUpdate(nextProps, nextState, nextContext) { // 是否支持本身的state和父组件传递的props数据更新,return true则为可以,false则不可以  return 必填
            console.log(nextProps)
            console.log(nextState)
            console.log('我是shouldComponentUpdate')
            return true
        }
        componentDidUpdate(prevProps, prevState, snapshot) { // 数据已经更新
            console.log('我是componentDidUpdate')
        }
        componentWillReceiveProps(nextProps, nextContext) { // 父组件的props数据更改
            console.log('我是componentWillReceiveProps')
        }
        componentDidCatch(error, errorInfo) { //  render()函数抛出错误,则会触发该函数
            console.log('我是componentDidCatch')
        }
        componentWillUnmount() { // 组件销毁前
        }
        changeMsg= ()=>{
            this.setState({
                value: 'aaaa'
            })
        }
        render() {
            console.log('我是render')
            return(
                <div>
                    <h3>{this.state.value}</h3>
                    <button onClick={this.changeMsg}>改变state中的值</button>
                </div>
            )
        }
    }
    export default LifeCycle
  • 相关阅读:
    leetcode46 Permutations
    leetcode75 Sort Colors
    leetcode347 Top K Frequent Elements
    目录文件的浏览、管理及维护(二).作业
    目录文件的浏览、管理及维护(一).作业
    Linux系统基础.作业
    补码原码反码
    第一次测试感想
    总结八
    假期总结七
  • 原文地址:https://www.cnblogs.com/cazj/p/11131768.html
Copyright © 2011-2022 走看看