一、属性
1、第一种使用方法:键值对
1a235929b06f5a41b46c176b10f79f6e
49df62249afd1b5109cb1b754f02b01d
1be2b9b453454e650a41135e640b08a2
eeb0256660d29d8f9799df27556f1081//数组
783b27803c3cf58f5e79eef5156f57cf //定义一个函数
2、第二种方法:三个点的展开对象形式
var props = {
one :”123”,
tow :321
}
<classnameb {…props} />
增加三个引号相当于这里面拿到两个属性了(one和two)
3、setprops形式:通过组件更新属性,不能在组件内部中修改属性的,因为会违背组件设计原则(尽量避免)
var instance =react.render(<classnamec ><claasnamec/>,document.body);
instance.setprops({name:”tom" });
二、状态:事物所处的状况,由事物自行处理不断变化/事物的私有属性
getinitialstate:初始化每个实例特有的状态
setstate:更新组件状态
setstate会触发diff算法:判断state和页面结果的区别,是否需要更新
三、状态和属性对比
状态和属性都会触发render更新,都是纯js对象
状态:是和自己相关的,既不受父组件也不受子组件影响
属性:本身是不能自己去修改的,只能从父组件获取属性,父组件也能修改它的属性
根本的区别:组件在运行时需要去修改维护的就是状态
四、简单的demo熟悉一下:
<!doctype html>
<html>
<head>
<meta http-equiv='content-type' content='text/html; charset=utf-8'>
<title>daomul's example</title>
<link rel="stylesheet" href="../shared/css/base.css" />
</head>
<body>
<h1>text demo</h1>
<div id="container">
</div>
<script src="../shared/thirdparty/es5-shim.min.js"></script>
<script src="../shared/thirdparty/es5-sham.min.js"></script>
<script src="../shared/thirdparty/console-polyfill.js"></script>
<script src="../../build/react.js"></script>
<script src="../../build/jsxtransformer.js"></script>
<script type="text/jsx">
//内容组件
var content = react.createclass({
getinitialstate:function(){
return {
inputtext:'',
};
},
handlechange:function(event){
this.setstate({inputtext:event.target.value});
},
handleclick:function(){
console.log("props name is " + this.props.selectname + " \n and inputtext is " + this.state.inputtext);
},
render:function(){
return <div>
<textarea onchange = {this.handlechange} placeholder = "please input something!"></textarea>
<button onclick = {this.handleclick}>sumbit</button>
</div>;
},
});
//评论组件
var comment = react.createclass({
getinitialstate:function(){
return {
names:["tom","axiba","daomul"],
selectname:'',
};
},
handleselect:function(){
this.setstate(
{selectname : event.target.value}
);
},
render:function(){
var options = [];
//往options中添加子option
for (var option in this.state.names) {
options.push(<option value={this.state.names[option]}> {this.state.names[option]} </option>)
};
return <div>
<content selectname = {this.state.selectname}>
</content>
<select onchange = {this.handleselect}>
{options}
</select>
</div>;
},
});
//start render
react.render(<comment></comment>,document.body);
</script>
</body>
</html>