react笔记组件属性传递机制(四)
程序员文章站
2024-02-26 15:39:46
...
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>复合式组件设计</title>
<script src="../build/react.js"></script>
<script src="../build/react-dom.js"></script>
<script src="../build/browser.min.js"></script>
<style type="text/css">
#example{
padding: 50px;
background: #fff;
}
</style>
</head>
<body>
<div id="example"></div>
<script type="text/babel">
var Display = React.createClass({
render:function () {
return (
<div>
<p>{this.props.color}</p>
<p>{this.props.num}</p>
<p>{this.props.size}</p>
</div>
)
}
})
//父组件
var Label = React.createClass({
render:function () {
return (
//...this.props 表示所有属性,"..." es6中对象的扩展运算符
<Display {...this.props}/>
)
}
})
var Shirt = React.createClass({
render:function () {
return (
//...this.props 表示所有属性,"..." es6中对象的扩展运算符
<Label {...this.props}/>
)
}
})
ReactDOM.render(
<div>
<Shirt color="deepblue" num = "3.14" size="medium"/>
</div>,
document.querySelector("#example"));
</script>
</body>
</html>
上一篇: 计算字节长度