react 生命周期 钩子函数(笔记 2)

五.  shouldComponentUpdate(){}  :   每当进行数据更新或页面重新渲染时会触发,当函数返回值为 true 时页面可以更新,当返回值为 false 时页面不会更新而会保持原有状态。

      class App extends React.Component{
        state ={m:0}
        
        shouldComponentUpdate(){
          console.log("----shouldComponentUpdate");
          return true;
        }

        render(){
          console.log("----render");
          return(
            <div>
               App :{this.state.m}
              <button onClick={
                ()=>{
                  this.setState({m:1})
                }
              }>更新</button>
              
              </div>
          
          )
        }
      }

shouldComponentUpdate(){} 函数可以做性能优化使用,在函数内部判断修改的数据是否跟原数据一致,如不一致则修改,一致则保持原有状态。

        shouldComponentUpdate(nextProps,nextState){
          console.log("----shouldComponentUpdate");
          if(this.state.m=== nextState.m)
              return false;
          return true;
        }

六.  componentWillUpdate(){}  :  组件每次更新前调用此钩子函数。


        componentWillUpdate(){
          console.log("----componentWillUpdate");
        }

由于安全性问题后改名为 UNSAFE_componentWillUpdate(){}

        UNSAFE_componentWillUpdate(){
          console.log("----UNSAFE_componentWillUpdate");
        }

 七. componentDidUpdate(){} :  组件每次更新完毕立即执行

        //第一次更新完毕执行
        componentDidUpdate(){
          console.log("----componentDidUpdate");
        }

八. componentWillUnmount(){} :  将要卸载函数,在将要卸载函数组件前调用

        componentWillUnmount(){
          console.log("----componentWillUnmount");
        } 
        render(){
          console.log("----render");
          return(
            <div>
               App :{this.state.m}

              <button onClick={
                ()=>{
                  ReactDOM.unmountComponentAtNode(document.getElementById('app'))
                }
              }>卸载</button>
              
              </div>
          
          )
        }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值