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

本文探讨了React中父组件更新导致子组件不必要的重新渲染问题,指出即使子组件状态未变也会触发渲染。为了解决这个问题,介绍了使用`shouldComponentUpdate`生命周期方法进行条件判断,以决定是否需要渲染。通过在`shouldComponentUpdate`中比较`nextProps`和`nextState`,可以避免不必要的组件更新,提高应用性能。
摘要由CSDN通过智能技术生成

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

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

解决方式 使用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'))

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端大歌谣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值