zoukankan      html  css  js  c++  java
  • React生命周期

    react生命周期

    image

    class Counter extends React.Component {
      static defaultProps = {
        name:'plus'
      }
      constructor(props){
         super(props)
         this.state = {
           number:0
         }
         console.log('构造函数')
      }
      componentWillMount(){
        console.log('组件将要加载')
      }
      componentDidMount(){
        console.log('组件挂载完成')
      }
      handleClick = () => {
        this.setState({
          number:this.state.number+1
        })
      }
      shouldComponentUpdate(nextProps,nextState){
        console.log('组件是否更新')
        return nextState.number % 2
        ///如果此函数种返回了false 就不会调用render方法了
      }
      componentWillUpdate(){
        console.log('组件将要更新')
      }
      componentDidUpdate(){
        console.log('组件更新完成')
      }
      render(){
        console.log('render')
        return (
          <div>
            <p>{this.state.number}</p>
            {this.state.number > 3 ? null :<ChildCounter n={this.state.number}/>}
              <button onClick={this.handleClick}>+</button>
          </div>
        )
      }
    }
    
    class ChildCounter extends React.Component{
      componentWillUnMount(){
        console.log('组件将要卸载componentWillUnmount')
      }
      componentWillMount(){
        console.log('child componentWillMount')
      }
      render(){
        console.log('child-render')
        return (<div>
          {this.props.n}
        </div>)
      }
      componentDidMount(){
        console.log('child componentDidMount')
      }
      componentWillReceiveProps(newProps){ // 第一次不会执行,之后属性更新时才会执行
        console.log('child componentWillReceiveProps')
      }
      shouldComponentUpdate(nextProps,nextState){
        return nextProps.n % 3; //子组件判断接收的属性 是否满足更新条件 为true则更新
      }
    }
    
    ReactDOM.render(<Counter/>,document.getElementById('root'))
    
  • 相关阅读:
    shared_ptr weak_ptr boost 内存管理
    _vimrc win7 gvim
    qt 拖放
    数学小魔术 斐波那契数列
    qt4 程序 移植到 qt5
    (转)字符串匹配算法总结
    c++11
    BM 字符串匹配
    编译qt5 demo
    c++ 类库 学习资源
  • 原文地址:https://www.cnblogs.com/pluslius/p/10198326.html
Copyright © 2011-2022 走看看