React入門之--組件屬性的傳遞

廢話不多說,上代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>屬性從最外層傳到最裏層的實現</title>
    <script src="../dist/browser.min.js"></script>
    <script src="../dist/jquery.js"></script>
    <script src="../dist/react.js"></script>
    <script src="../dist/react-dom.js"></script>
</head>
<body>
<div id="container"></div>
<script type="text/babel">
    var destination = document.querySelector('#container');
    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 (
                //簡單寫法
                <Display {...this.props}/>

                // <Display color={this.props.color} num={this.props.num} size={this.props.size}></Label>
            );
        }
    });
    var Shirt = React.createClass({
        render: function () {
            return (
                <Label {...this.props}/>
                // <Label color={this.props.color} num={this.props.num} size={this.props.size}></Label>
            )
        }
    })


    ReactDOM.render(
        <div>
            <Shirt color="deepblue" num="3.14" size="medium"></Shirt>
        </div>,
        destination
    )
</script>
</body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章