【React学习】—组件三大核心属性: state(七)

【React学习】—组件三大核心属性: state(七)

在这里插入图片描述

2.2.2. 理解

  1. state是组件对象最重要的属性, 值是对象(可以包含多个key-value的组合)
  2. 组件被称为"状态机", 通过更新组件的state来更新对应的页面显示(重新渲染组件)

2.2.3. 强烈注意

  1. 组件中render方法中的this为组件实例对象
  2. 组件自定义的方法中this为undefined,如何解决?
    a) 强制绑定this: 通过函数对象的bind()
    b) 箭头函数
  3. 状态数据,不能直接修改或更新
    在这里插入图片描述
      <script type="text/babel">
      class Weather extends React.Component{
    
    

        constructor(props){
    
    
            super(props)
                this.state={
    
    isHot:false}
        }

        render() {
    
    
            return <h1>今天天气很{
    
    this.state.isHot?'炎热':'凉爽'}</h1>
        }
      }

    ReactDOM.render(<Weather/>,document.getElementById('test'))
    </script>

猜你喜欢

转载自blog.csdn.net/m0_46374969/article/details/132294158