好客租房81-组件性能优化(减轻state)

减轻state 只存储更组件渲染相关的数据(count/列表数据/loading等)

注意:不用做渲染的数据不要放在state中 比如定时器id等

对于这种需要在多个方法用到的数据 应该放在this中

    //导入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)
        }
        render() {
            console.log('生命周期钩子函数:render')
            console.log(this.props,"props")
            return (
                <div id="title">
                                  
                </div>
            )
        }
    }
     
     
     
    ReactDOM.render(<App></App>, document.getElementById('root'))
 

posted @   前端导师歌谣  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示