当前位置: 首页 > news >正文

React 重识

基本基础:css html JavaScript

JS 中的类

ES5之前使用function创建一个类

ES6之后引入class关键字

class Person{constructor(name,age){this.name = namethis.age = age}running(){}}const p = new Person(`why`,18)

ES6 中类的继承 继承是多态的前提

子类中必须通过super()初始化父类的对象

列表循环遍历渲染

// const About = () => {//     return <div>我是关于 </div>
// }
import React from "react"
class About extends React.Component{constructor(){super()this.state = {message:`电影列表`,movies:["fender","jazzmaster","jaguar"]}}render(){const list = []for(let movie of this.state.movies){list.push(<li>{movie}</li>)}return <div><h2>{this.state.message}</h2><ul>{list}</ul></div>}
}
export default About

方法二:使用map函数循环渲染

// const About = () => {//     return <div>我是关于 </div>
// }
import React from "react"
class About extends React.Component{constructor(){super()this.state = {message:`电影列表`,movies:["fender","jazzmaster","jaguar","brighton"]}}render(){// const list = []// for(let movie of this.state.movies){//     list.push(<li>{movie}</li>)// }return <div><h2>{this.state.message}</h2><ul>{this.state.movies.map((item,index)=><li key={index}>{item}</li>)}</ul></div>}
}
export default About

类组件实现计数器

// const About = () => {//     return <div>我是关于 </div>
// }
import React from "react"
class About2 extends React.Component{constructor(){super()this.state = {message:`电影列表`,movies:["fender","jazzmaster","jaguar","brighton"]}}render(){// const list = []// for(let movie of this.state.movies){//     list.push(<li>{movie}</li>)// }return <div><h2>{this.state.message}</h2><ul>{this.state.movies.map((item,index)=><li key={index}>{item}</li>)}</ul></div>}
}class About extends React.Component{constructor(props){super(props)this.state = {counter:0}}increase(){console.log(`加一起`)this.setState({counter:this.state.counter+1})}decrease(){console.log(`减小`)console.log(this)this.setState({counter:this.state.counter-1})}render(){return <div><h2>当前计数:{this.state.counter}</h2><button onClick={this.increase.bind(this)}>增加+</button><button onClick={this.decrease.bind(this)}>减小-</button></div>}
}
export default About
http://www.lqws.cn/news/465931.html

相关文章:

  • Seata模式
  • Spring AOP全面详讲
  • 从 Elasticsearch 集群中移除一个节点
  • `customRef` 在实战中的使用:防抖、计算属性缓存和异步数据获取
  • 腾讯云IM即时通讯:开启实时通信新时代
  • nuxt3 + vue3 分片上传组件全解析(支持大文件+断点续传)
  • RabbitMQ 的工作流程
  • 【unitrix】 3.6 类型级数转基础类型(from.rs)
  • springboot通过独立事务管理器实现资源隔离与精准控制​
  • HTTPS的加密方式介绍
  • MinIO社区版文件预览失效?一招解决
  • 【Fargo】mediasoup发送2:码率分配、传输基类设计及WebRtcTransport原理
  • React 组件通信
  • C++ 移动构造:提升性能的利器
  • docker执行yum报错Could not resolve host: mirrorlist.centos.org
  • Snapchat矩阵运营新策略:亚矩阵云手机打造高效社交网络
  • C++:动态链接库的编写,__declspec 用法详解
  • 7.3.2_2平衡二叉树的删除
  • 【RTP】基于mediasoup的RtpPacket的H.264打包、解包和demo 1:不含扩展
  • windows下docker虚拟文件大C盘迁移D盘
  • GPT-1 与 BERT 架构
  • TodoList 案例(Vue3): 使用Composition API
  • 基于CNN-LSTM融合模型的环卫车动态称重算法研究:从频率感知到精准质量估计
  • 深入浅出JavaScript 中的代理模式:用 Proxy 掌控对象的“行为开关”
  • Python 爬虫案例(不定期更新)
  • Occt几何内核快速入门
  • Duende Identity Server学习之一:认证服务器及一个Oidc/OAuth认证、用于Machine 2 Machine的客户端
  • 在Docker、KVM、K8S常见主要命令以及在Centos7.9中部署的关键步骤学习备存
  • stm32移植freemodbus
  • C++ - vector 的使用