section-1

//react组件
export class Halo extends React.Component{
constructor(...args){
super(...args); //初始化父类构造函数
this.state={ //设置state
text:""
}
}
hello(ev){
this.setState({ //修改state
text:ev.target.value
})
}
render(){
return(
{/*只能一个父元素包裹*/}
<div>
{/*事件大小写注意 onChange onClick*/}
<input type="text" onChange= {this.hello.bind(this)}/>
<span>{this.state.text}</span>
</div>
)
}
}

section-2

export class Halo extends React.Component{
constructor(...args){
super(...args);
this.state={
display:"block"
}
}
toggle(){
this.setState({
display:this.state.display==="none" ? "block":"none"
})
}
render(){
return(
<div>
<input type="button" value="切换" onClick={this.toggle.bind(this)}/>
{/*class在JSX中需要改成className*/}
{/*行内样式需要加两个花括号*/}
<span className="content" style={{display:this.state.display}}>内容的显示和隐藏</span>
</div>
)
}
}

section-3

export class Halo extends React.Component{
constructor(...args){
super(...args);
this.state={
h:0,
m:0,
s:0
};
setInterval(function(){
this.update();
}.bind(this),1000)
}
update(){
let date=new Date();
this.setState({
h:date.getHours(),
m:date.getMinutes(),
s:date.getSeconds()
})
}
componentDidMount(){
this.update();
}
render(){
return(
<div>
{this.state.h}:{this.state.m}:{this.state.s}
</div>
)
}
}

section-4

/*react 生命周期*/

componentWillMount()   创建前
componentDidMount() 创建后 componentWillUpdate() 更新前
componentDidUpdate() 更新后 componentWillUnMount() 销毁前 componentWillReceiveProps(nextProps){
// ???
} shouldComponentUpdate(){
//返回boolean值 默认每次状态更改时重新渲染
//返回false componentWillUpdate(),render()和componentDidUpdate()将不会被调用
}

section-5

//组件的另一种写法 无状态state组件
//Es6 React.Component Es5 React.createClass 其他两种定义方式
let Item=function(props){
return <li>{props.value}</li>
}; export class Halo extends React.Component{
constructor(...args){
super();
this.state={
arr:[1,2,3,4,5]
}
}
addItem(){
this.setState({
arr:this.state.arr.concat([Math.random()])
})
}
render(){
let result=[],arr=this.state.arr;
for(var i=0;i<arr.length;i++){
{/*需要给每个Item增加unique key唯一标识*/}
result.push(<Item key={i} value={arr[i]} />)
}
return(
<div>
<input type="button" value="增加Item" onClick={this.addItem.bind(this)}/>
<ul>
{result}
</ul>
</div>
)
}
}

React基础知识备忘的更多相关文章

  1. java基础知识备忘

    1.java内存分配 a.寄存器cup -- 暂不涉及 b.本地方法栈  -- 虚拟机调用windows功能用的,比如创建文件夹 c.方法区  -- 存放 .class文件,负责存放方法 d.栈 -- ...

  2. JVM内存知识备忘

    又是一篇备忘... 主要记录一些知识,进行一些资源的汇总. 先来群里liufor大大提供的两张图,清晰易懂: Dockerized Java https://www.youtube.com/watch ...

  3. GIS部分理论知识备忘随笔

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.高斯克吕格投影带换算 某坐标的经度为112度,其投影的6度带和3度带 ...

  4. React基础知识

    学习文档(按优先级排列)http://reactjs.cn/react/docs/tutorial-zh-CN.htmlhttp://www.cnblogs.com/Mrs-cc/p/4969755. ...

  5. React 基础知识总结

    data-id="1190000016885142" data-license=""> 一.Node.js Node.js并不是一个JavaScript框 ...

  6. React开发实时聊天招聘工具 -第三章 React基础知识回顾

    function a (props) { return <h1>hello world{this.props.asd}</h1> } class a extends React ...

  7. ASP.NET基础代码备忘

    使用ASP.NET原生的__doPostBack方法触发asp:Button //javaScript部分 __doPostBack('<%=btnAmountDivided.UniqueID ...

  8. xml 基础学习备忘

    <?xml version="1.0" encoding="UTF-8"? standalone="yes"> 这里的encod ...

  9. JavaScript中JSONObject和JSONArray相关知识备忘(网络转载)

    1.json的格式,有两种: {"key": "value"} //JSONObject(对象) [{"key1": "value ...

随机推荐

  1. 第20月第4天 pycharm utf-8

    1.运行python %run a.py 运行 https://blog.csdn.net/little_bobo/article/details/78982412 2.UnicodeDecodeEr ...

  2. Kaldi的BaseLine训练过程

    steps/train_mono.sh --nj "$train_nj" --cmd "$train_cmd" data/train data/lang exp ...

  3. python之字典的增删改查

    Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型.字典都是无序的,但查询速度快. 字典是一个key/value的集合,key可以是任意可被哈希(内部key ...

  4. undefined symbol

    参考链接:  https://blog.csdn.net/shatterheart/article/details/52440149

  5. Spark架构与作业执行流程简介

    https://www.cnblogs.com/shenh062326/p/3658543.html

  6. ASP.NET MVC - 模型与元数据(进阶)

    模型与元数据(Model & Model Metadata) ASP.NET MVC中有一个模型的概念,实际上模型就是一个类型,Model表示的就是最终要绑定到View视图页面上的数据而已. ...

  7. Web方面的错误, 异常来自hresult:0x80070057(E_INVALIDARG)

    删除 C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET files 这个文件夹. 解决方法: 1.代码保存频繁一点.做一个 ...

  8. java知识点4

    架构篇 分布式 数据一致性.服务治理.服务降级 分布式事务 2PC.3PC.CAP.BASE. 可靠消息最终一致性.最大努力通知.TCC Dubbo 服务注册.服务发现,服务治理 分布式数据库 怎样打 ...

  9. How to Train Triplet Networks with 100K Identities?

    1. 为什么介绍此文? Triplet net 改进工作之一,主要思想是在大数据集(人脸识别)上的困难样本挖掘.人脸识别工作对于图像对匹配而言很有借鉴意义,共性是特征的提取和样本数据的挖掘. Trip ...

  10. [Codeforces671D]Roads in Yusland

    [Codeforces671D]Roads in Yusland Tags:题解 题意 luogu 给定以1为根的一棵树,有\(m\)条直上直下的有代价的链,求选一些链把所有边覆盖的最小代价.若无解输 ...