state的简写方式
<!DOCTYPE html>
<html>
<head>
? ? <meat charset="UTF-8">
? ? ? ? <title>state的簡寫方式</title>
</head>
<body>
? ? <!-- 準(zhǔn)備好一個容器 -->
? ? <div id="test"></div>
? ? <!-- 核心庫 -->
? ? <script type="text/javascript" src="../js/react.development.js"></script>
? ? <!-- react-dom,操作dom -->
? ? <script type="text/javascript" src="../js/react-dom.development.js"></script>
? ? <!-- 引入babel,jsx轉(zhuǎn)為js -->
? ? <script type="text/javascript" src="../js/babel.min.js"></script>
? ? <!-- 此處一定要寫babel -->
? ? <script type="text/babel">
? ? ? ? //1.創(chuàng)建組件
? ? ? ? class Weather extends React.Component{
? ? ? ? ? ? //初始化狀態(tài)
? ? ? ? ? ? state = { isHot: false, wind: '微風(fēng)' } ?
? ? ? ? ? ?render(){ ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?const {isHot,wind} =this.state; ? ? ? ?
? ? ? ? ? ? ? ?return <h1 onClick={this.changeWeather}>今天天氣很{isHot ? '炎熱' : '涼爽'},{wind}</h1>
? ? ? ? ? ?} ? ?
? ? ? ? ? //自定義方法————要用賦值語句的方式+箭頭函數(shù)
? ? ? ? ? ? changeWeather = ()=> { ? ? ? ? ?
? ? ? ? ? ? ? ? const isHot=this.state.isHot ? ? ? ? ? ?
? ? ? ? ? ? ? ? this.setState({isHot:!isHot})
? ? ? ? ? ? }
? ? ? ? } ?
? ? ? ? //2.渲染組件到頁面
? ? ? ? ReactDOM.render(<Weather />, document.getElementById('test')); ?
? ? </script>
</body>
</html>
?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的state的简写方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全网最细之super讲解
- 下一篇: 多线程:CopyOnWriteArray