0
点赞
收藏
分享

微信扫一扫

好客租房82-组件性能优化(2减少不必要的渲染)


组件更新机制:父组件更新也会引起子组件也被更新 这种思路很清晰

子组件没有任何变化的时候也会重新渲染

解决方式 使用shouldComponentUpdate(nextProps,nextState)

nextProps最新的props

nextState最新的状态

shouldComponentUpdate参数更新完之后渲染render

   

//导入react
import React from 'react'
import ReactDOM from 'react-dom'

//导入组件
// 约定1:类组件必须以大写字母开头
// 约定2:类组件应该继承react.component父类 从中可以使用父类的方法和属性
// 约定3:组件必须提供render方法
// 约定4:render方法必须有返回值

class App extends React.Component {
constructor(props) {
super(props)
console.log('生命周期钩子函数:construtor')
}
componentDidMount=()=>{
this.timeId=setInterval(()=>{

},2000)
}
componentWillUnmount(){
clearInterval(this.timeId)
}
shouldComponentUpdate(){
//根据条件判断渲染true或者false

}
render() {
console.log('生命周期钩子函数:render')
console.log(this.props,"props")
return (
<div id="title">

</div>
)
}
}



ReactDOM.render(<App></App>, document.getElementById('root'))

举报

相关推荐

0 条评论