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

2.2.2. 理解
- state是组件对象最重要的属性, 值是对象(可以包含多个key-value的组合)
 - 组件被称为"状态机", 通过更新组件的state来更新对应的页面显示(重新渲染组件)
 
2.2.3. 强烈注意
- 组件中render方法中的this为组件实例对象
 - 组件自定义的方法中this为undefined,如何解决?
a) 强制绑定this: 通过函数对象的bind()
b) 箭头函数 - 状态数据,不能直接修改或更新

 
      <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>
                


















