欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          react新舊生命周期的區(qū)別是什么

          react新舊生命周期的區(qū)別:1、新生命周期中去掉了三個(gè)will鉤子,分別為componentWillMount、componentWillReceiveProps、componentWillUpdate;2、新生命周期中新增了兩個(gè)鉤子,分別為getDerivedStateFromProps(從props中得到衍生的state)和getSnapshotBeforeUpdate。

          react新舊生命周期的區(qū)別是什么

          本教程操作環(huán)境:Windows7系統(tǒng)、react18版、Dell G3電腦。

          react在版本16.3前后存在兩套生命周期,16.3之前為舊版,之后則是新版,雖有新舊之分,但主體上大同小異。

          React生命周期(舊)

          react新舊生命周期的區(qū)別是什么

          值得強(qiáng)調(diào)的是:componentWillReceiveProps函數(shù)在props第一次傳進(jìn)來時(shí)不會(huì)調(diào)用,只有第二次后(包括第二次)傳入props時(shí),才會(huì)調(diào)用

          shouldComponentUpdate像一個(gè)閥門,需要一個(gè)返回值(true or false)來確定本次更新的狀態(tài)是不是需要重新render

          React生命周期(新)

          react新舊生命周期的區(qū)別是什么

          react新舊生命周期的區(qū)別

          新的生命周期去掉了三個(gè)will鉤子,分別是:componentWillMount、componentWillReceiveProps、componentWillUpdate

          新的生命周期新增了兩個(gè)鉤子,分別是:

          1、getDerivedStateFromProps:從props中得到衍生的state

          • 接受兩個(gè)參數(shù):props,state

          • 返回一個(gè)狀態(tài)對象或者null,用來修改state的值。

          • 使用場景:若state的值在任何時(shí)候都取決于props,那么可以使用getDerivedStateFromProps

          2、getSnapshotBeforeUpdate:在更新前拿到快照(可以拿到更新前的數(shù)據(jù))

          在更新DOM之前調(diào)用

          返回一個(gè)對象或者null,返回值傳遞給componentDidUpdate

          componentDidUpdate():更新DOM之后調(diào)用

          • 接受三個(gè)參數(shù):preProps,preState,snapshotValue

          使用案例:

          固定高度的p,定時(shí)新增一行,實(shí)現(xiàn)在新增的時(shí)候,使目前觀看的行高度不變。

          <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>4_getSnapShotBeforeUpdate的使用場景</title> <style> .list{ width: 200px; height: 150px; background-color: skyblue; overflow: auto; } .news{ height: 30px; } </style> </head> <body> <!-- 準(zhǔn)備好一個(gè)“容器” --> <div id="test"></div>  <!-- 引入react核心庫 --> <script type="text/javascript" src="../js/17.0.1/react.development.js"></script> <!-- 引入react-dom,用于支持react操作DOM --> <script type="text/javascript" src="../js/17.0.1/react-dom.development.js"></script> <!-- 引入babel,用于將jsx轉(zhuǎn)為js --> <script type="text/javascript" src="../js/17.0.1/babel.min.js"></script>   <script type="text/babel"> class NewsList extends React.Component{   state = {newsArr:[]}   componentDidMount(){ setInterval(() => { //獲取原狀態(tài) const {newsArr} = this.state //模擬一條新聞 const news = '新聞'+ (newsArr.length+1) //更新狀態(tài) this.setState({newsArr:[news,...newsArr]}) }, 1000); }   getSnapshotBeforeUpdate(){ return this.refs.list.scrollHeight }   componentDidUpdate(preProps,preState,height){ this.refs.list.scrollTop += this.refs.list.scrollHeight - height }   render(){ return( <div className="list" ref="list"> { this.state.newsArr.map((n,index)=>{ return <div key={index} className="news">{n}</div> }) } </div> ) } } ReactDOM.render(<NewsList/>,document.getElementById('test')) </script> </body> </html>

          說明:

          在React v16.3中,迎來了新的生命周期改動(dòng)。舊的生命周期也在使用,不過在控制臺(tái)上可以看到棄用警告了。并且提示有三個(gè)生命周期鉤子將會(huì)被棄用,盡量不要使用。再或者可以在其前邊加前綴 UNSAFE_

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)