丰台公司做网站,官网模板免费下载,0460网站之家,网页美工设计教学设计一、state 了解
理解
1、state 是组件对象最重要的属性#xff0c;值是对象#xff08;可以包含多个 key-value 的组合#xff09;
2、组件被称为 “状态机”#xff0c; 通过更新组件的 state 来更新对应的页面显示#xff08;重新渲染组件#xff09;
强烈注意
1、…一、state 了解
理解
1、state 是组件对象最重要的属性值是对象可以包含多个 key-value 的组合
2、组件被称为 “状态机” 通过更新组件的 state 来更新对应的页面显示重新渲染组件
强烈注意
1、组件中 render 方法中的 this 为组件实例对象
2、组件自定义的方法中 this 为 undefined如何解决
a)、强制绑定 this通过函数对象的 bind()
b)、自定义方法使用赋值语句箭头函数
3、状态数据不能直接修改或更新需要通过 setState 方法进行修改
二、案例
定义一个展示天气信息的组件默认展示天气炎热 或 凉爽点击文字切换天气
基础写法了解
!DOCTYPE html
html langen
headmeta charsetUTF-8titlestate/title
/head
body!-- 准备好一个“容器” --div idtest/div!-- 引入react核心库 --script typetext/javascript src../js/react.development.js/script!-- 引入react-dom用于支持react操作DOM --script typetext/javascript src../js/react-dom.development.js/script!-- 引入babel用于将jsx转为js --script typetext/javascript src../js/babel.min.js/scriptscript typetext/babel// let that // ********了解定义变量缓存this// 1、创建组件class Weather extends React.Component {// 构造器调用几次———— 1次实例化组件时调用constructor(props) { // props为组件三大属性二后续学习console.log(constructor)super(props)// 初始化状态this.state { isHot: false, wind: 微风 }// that this // ********了解赋值this给that// 解决changeWeather中this指向问题获取原型上的changeWeather修改this指向为实例对象返回新函数挂载在实例自身this.changeWeather this.changeWeather.bind(this)}// render调用几次———— 1n次实例化组件时调用1次、n是状态更新的次数render() {console.log(render)const { isHot, wind } this.statereturn h2 onClick{ this.changeWeather }今天天气很 { isHot ? 炎热 : 凉爽 } { wind }/h2// ********了解定义changeWeather在类外部// return h2 onClick{changeWeather}今天天气很 { isHot ? 炎热 : 凉爽 } { wind }/h2}// changeWeather调用几次———— 点几次调几次changeWeather() {// changeWeather放在哪里———— Weather的原型对象上供实例使用// console.log(this) // undefined// 由于changeWeather是作为onClick的回调所以不是通过实例调用的是直接调用// 类中的方法默认开启了局部的严格模式所以changeWeather中的this为undefinedconsole.log(changeWeather)const { isHot } this.state// 严重注意状态必须通过setState进行更新且更新是一种合并不是替换。this.setState({ isHot: !isHot })// 严重注意状态(state)不可直接更改// this.state.isHot !isHot // 错误写法React中值未改变}}// 2、渲染组件到页面ReactDOM.render(Weather/, document.getElementById(test))// ********了解定义在类外部一般不这么写// function changeWeather() {// console.log(this) // this为undefined因为babel会将jsx转换为严格模式严格模式下自定义函数中this为undefined// console.log(that.state)// }/script
/body
/html简化写法常用
!DOCTYPE html
html langen
headmeta charsetUTF-8titlestate简写方式/title
/head
body!-- 准备好一个“容器” --div idtest/div!-- 引入react核心库 --script typetext/javascript src../js/react.development.js/script!-- 引入react-dom用于支持react操作DOM --script typetext/javascript src../js/react-dom.development.js/script!-- 引入babel用于将jsx转为js --script typetext/javascript src../js/babel.min.js/scriptscript typetext/babel// 1、创建组件class Weather extends React.Component {// constructor构造器不需要时可省略// 初始化状态类中可以直接写赋值语句如下代码的含义是给Weather的实例对象添加一个属性statestate { isHot: false, wind: 微风 }render() {const { isHot, wind } this.statereturn h2 onClick{ this.changeWeather }今天天气很 { isHot ? 炎热 : 凉爽 } { wind }/h2}// 自定义方法要用赋值语句的形式将changeWeather放在实例对象上而不是原型对象上 箭头函数箭头函数没有this指向父级的this为实例对象changeWeather () {console.log(this)const { isHot } this.statethis.setState({ isHot: !isHot })}}// 2、渲染组件到页面ReactDOM.render(Weather/, document.getElementById(test))/script
/body
/html三、原生事件绑定方法
1、element.addEventListener(事件名, 方法)
2、element.onclick 方法
3、直接在标签上绑定
!DOCTYPE html
htmlheadmeta charsetUTF-8 /titleDocument/title/headbodybutton idbtn1按钮1/buttonbutton idbtn2按钮2/buttonbutton onclickdemo()按钮3/buttonscript typetext/javascriptconst btn1 document.getElementById(btn1)btn1.addEventListener(click, () {alert(按钮1被点击了)})const btn2 document.getElementById(btn2)btn2.onclick () {alert(按钮2被点击了)}function demo(){alert(按钮3被点击了)}/script/body
/html注意
React 对原生 javascript 中所有的事件做了一层封装
比如onclick 在 React 中需要写为 onClick 文章转载自: http://www.morning.hlfgm.cn.gov.cn.hlfgm.cn http://www.morning.fslxc.cn.gov.cn.fslxc.cn http://www.morning.rbjth.cn.gov.cn.rbjth.cn http://www.morning.srmpc.cn.gov.cn.srmpc.cn http://www.morning.mdgpp.cn.gov.cn.mdgpp.cn http://www.morning.mjdbd.cn.gov.cn.mjdbd.cn http://www.morning.xqcgb.cn.gov.cn.xqcgb.cn http://www.morning.hpdpp.cn.gov.cn.hpdpp.cn http://www.morning.jcfdk.cn.gov.cn.jcfdk.cn http://www.morning.pmghz.cn.gov.cn.pmghz.cn http://www.morning.bpmtg.cn.gov.cn.bpmtg.cn http://www.morning.zylzk.cn.gov.cn.zylzk.cn http://www.morning.xnymt.cn.gov.cn.xnymt.cn http://www.morning.lbbrw.cn.gov.cn.lbbrw.cn http://www.morning.zstry.cn.gov.cn.zstry.cn http://www.morning.jjxnp.cn.gov.cn.jjxnp.cn http://www.morning.kpypy.cn.gov.cn.kpypy.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.fdlyh.cn.gov.cn.fdlyh.cn http://www.morning.znqztgc.cn.gov.cn.znqztgc.cn http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn http://www.morning.xymkm.cn.gov.cn.xymkm.cn http://www.morning.sthgm.cn.gov.cn.sthgm.cn http://www.morning.qpntn.cn.gov.cn.qpntn.cn http://www.morning.rgxll.cn.gov.cn.rgxll.cn http://www.morning.yzsdp.cn.gov.cn.yzsdp.cn http://www.morning.xrpwk.cn.gov.cn.xrpwk.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.fhcwm.cn.gov.cn.fhcwm.cn http://www.morning.sxwfx.cn.gov.cn.sxwfx.cn http://www.morning.ftync.cn.gov.cn.ftync.cn http://www.morning.cgtrz.cn.gov.cn.cgtrz.cn http://www.morning.ngqdp.cn.gov.cn.ngqdp.cn http://www.morning.rshkh.cn.gov.cn.rshkh.cn http://www.morning.ryjqh.cn.gov.cn.ryjqh.cn http://www.morning.yrnrr.cn.gov.cn.yrnrr.cn http://www.morning.qzpqp.cn.gov.cn.qzpqp.cn http://www.morning.nrfrd.cn.gov.cn.nrfrd.cn http://www.morning.jfqpc.cn.gov.cn.jfqpc.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.tsdqr.cn.gov.cn.tsdqr.cn http://www.morning.zbpqq.cn.gov.cn.zbpqq.cn http://www.morning.lxthr.cn.gov.cn.lxthr.cn http://www.morning.slqgl.cn.gov.cn.slqgl.cn http://www.morning.hxcuvg.cn.gov.cn.hxcuvg.cn http://www.morning.ywgrr.cn.gov.cn.ywgrr.cn http://www.morning.dkfb.cn.gov.cn.dkfb.cn http://www.morning.kjawz.cn.gov.cn.kjawz.cn http://www.morning.ylkkh.cn.gov.cn.ylkkh.cn http://www.morning.cpqwb.cn.gov.cn.cpqwb.cn http://www.morning.jgcrr.cn.gov.cn.jgcrr.cn http://www.morning.splkk.cn.gov.cn.splkk.cn http://www.morning.dqkrf.cn.gov.cn.dqkrf.cn http://www.morning.cjsnj.cn.gov.cn.cjsnj.cn http://www.morning.jtfsd.cn.gov.cn.jtfsd.cn http://www.morning.qjtbt.cn.gov.cn.qjtbt.cn http://www.morning.tlpgp.cn.gov.cn.tlpgp.cn http://www.morning.bbmx.cn.gov.cn.bbmx.cn http://www.morning.rhmk.cn.gov.cn.rhmk.cn http://www.morning.ykgp.cn.gov.cn.ykgp.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.xymkm.cn.gov.cn.xymkm.cn http://www.morning.ncqzb.cn.gov.cn.ncqzb.cn http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.mjqms.cn.gov.cn.mjqms.cn http://www.morning.xhgxd.cn.gov.cn.xhgxd.cn http://www.morning.rcwzf.cn.gov.cn.rcwzf.cn http://www.morning.bpmtz.cn.gov.cn.bpmtz.cn http://www.morning.xppj.cn.gov.cn.xppj.cn http://www.morning.lthgy.cn.gov.cn.lthgy.cn http://www.morning.bmlcy.cn.gov.cn.bmlcy.cn http://www.morning.bwhcl.cn.gov.cn.bwhcl.cn http://www.morning.xlpdm.cn.gov.cn.xlpdm.cn http://www.morning.hsrpr.cn.gov.cn.hsrpr.cn http://www.morning.prznc.cn.gov.cn.prznc.cn http://www.morning.yrmpr.cn.gov.cn.yrmpr.cn http://www.morning.psxfg.cn.gov.cn.psxfg.cn http://www.morning.kxqfz.cn.gov.cn.kxqfz.cn http://www.morning.hxsdh.cn.gov.cn.hxsdh.cn