本文主要是介绍React 学习笔记 —— 事件绑定和 state 属性修改简写版,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="../js/react.development.js"></script><script src="../js/react-dom.development.js"></script><script src="../js/babel.min.js"></script>
</head>
<body><div id="app"></div><script type="text/babel">class Weather extends React.Component {// 类中的赋值语句,state = xxx 相当于 this.state = xxxstate = {isHot: false, wind: '微风'}render () {const {isHot, wind} = this.statereturn <h1 onClick={this.changeWeather}>今天天气很{isHot ? '炎热' : '凉爽'}. {wind}</h1>}// 箭头函数内部没有 this,访问外层 this,而类中外层 this 为类的实例changeWeather = () => {const {isHot} = this.statethis.setState({isHot: !isHot})}}ReactDOM.render(<Weather/>, document.getElementById('app'))</script>
</body>
</html>
这篇关于React 学习笔记 —— 事件绑定和 state 属性修改简写版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!