一,组件

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css'; // 函数式组件 return
function Hello(props) {
let titleName = <p> 这是 Hello副标题 </p> return (
<div> <h1>今天天气:{props.weather} </h1> {titleName} </div>
) //在组件中 必须返回一个合法的虚拟jsx dom 元素
}
// 类组件
class Hi extends React.Component {
render() {
return (
<div><h1>今天天气:{this.props.weather} </h1></div>
)
}
}
ReactDOM.render(
<div>
<Hello weather="出太阳" />
<Hi weather="出太阳"/>
</div>,
document.getElementById('root')
);

二,组件状态state

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css'; //这种写法组件不能自动更新 class Hello extends React.Component {
// 状态-私有
constructor(props) {
super(props)
this.state = {
time:new Date().toLocaleTimeString()
}
}
// 视图
render() {
// 下面不加eslint 报错
// eslint-disable-next-line
this.state.time = new Date().toLocaleTimeString()
return (
<div><h1>当前时间:{this.state.time}</h1></div>
)
}
} ReactDOM.render(
<div>
<Hello />
</div>,
document.getElementById('root')
); setInterval(()=>{
// 反复渲染同一组件,不会重复渲染,所以要在render 的时候重新赋值
ReactDOM.render(
<div>
<Hello />
</div>,
document.getElementById('root')
);
},1000)
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css'; class Hello extends React.Component {
// 状态-私有
constructor(props) {
super(props)
this.state = {
time:new Date().toLocaleTimeString()
}
console.log(this.state.time)
}
// 视图
render() {
return (
<div><h1>当前时间:{this.state.time}</h1></div>
)
}
// 生命周期
componentDidMount() {
setInterval(()=>{
// setState()
this.setState({time:new Date().toLocaleTimeString()})
},1000)
}
} ReactDOM.render(
<div>
<Hello />
</div>,
document.getElementById('root')
)

三,点击事件

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css'; class Hello extends React.Component {
constructor(props) {
super(props)
this.state = {
c1:'active',
c2:'content'
}
// 改变show1 的this 指向
this.show1 = this.show1.bind(this)
}
render() {
return (
<div>
<button onClick={this.show}>不传参写法</button>
<button onClick={()=>this.show('1')}>一</button>
<button onClick={()=>this.show('2')}>二</button>
{/* 此时show1 的this 指向的是button 所以要改一下指向 */}
<button data-index="1" onClick={this.show1}>一</button>
<button data-index="2" onClick={this.show1}>二</button>
<div className={this.state.c1}>内容一</div>
<div className={this.state.c2}>内容二</div>
</div>
)
}
show(arg) {
console.log(arg)
if(arg === '1') {
this.setState({
c1:'active',
c2:'content'
})
}else {
this.setState({
c2:'active',
c1:'content'
})
} }
show1(e) {
console.log(e.target.dataset.index)
let arg = e.target.dataset.index
if(arg === '1') {
this.setState({
c1:'active',
c2:'content'
})
}else {
this.setState({
c2:'active',
c1:'content'
})
}
}
} ReactDOM.render(
<div>
<Hello />
</div>,
document.getElementById('root')
)

最新文章

  1. [CodeWars][JS]如何判断给定的数字是否整数
  2. DB2常用命令
  3. LeetCode: Queue Reconstruction by Height
  4. modelsim(3) - summary ,issue,tips
  5. linux source与 . 命令
  6. GC-垃圾回收
  7. D3D9 优化小技巧
  8. (转载)Java NIO:NIO概述(一)
  9. Android开发工具GenyMotion安装和使用方法
  10. SQLServer2008/2012 删除所有表视图存储过程
  11. C++一些注意点之转换操作符
  12. PHP中foreach()用法汇总
  13. [DeeplearningAI笔记]ML strategy_1_1正交化/单一数字评估指标
  14. 从感知机到 SVM,再到深度学习(一)
  15. VS2010动态链接库的生成及调用(C++)
  16. 让 Windows7 - 64bit 支持 VC++ 6.0 的解决方法(无法启动此程序,因为计算机中丢失 MSVCRTD.dll。尝试重新安装该程序以解决此问题)
  17. java实现文章敏感词过滤检测
  18. Linux(CentOs 7)系统重装笔记(二)---完全删除用户账号和root用户登录
  19. SpringBoot入门 (八) Cache使用
  20. Scrum团队 《构建之法》第6~7章

热门文章

  1. JavaScript:操作符:正负号和自增自减及其隐式转换数据类型
  2. CTFshow——funnyrsa3
  3. 阿里云Imagine Computing创新技术大赛决赛启幕!
  4. 再讲Floyd变形:传递闭包类问题
  5. win32com操作word API精讲 第六集 Range(四)对齐和缩进
  6. 通过一个示例形象地理解C# async await异步
  7. 行为型模式 - 迭代器模式iterator
  8. 【LeetCode】三数之和+四数之和(双指针)
  9. ES6 01 简介
  10. C#写文本日志