JSX使用、写法


```html
<!DOCTYPE html>
<lang lang="en">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            .para {
                font-size: 20px;
                color: green;
                text-decoration: line-through;
            }

            .active {
                color: red;
            }
        </style>
    </head>

    <body>
        <div id="app"></div>


        <script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin></script>
        <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin></script>
        <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

        <script type="text/babel">  //这里type="text/babel"不能省略,否则无法使用jsx

            class App extends React.Component {
                constructor(props) {
                    super(props);
                    this.state = {
                        firstname: "kobe",
                        lastname: "bryant",
                        isLogin: true
                    }
                }

                // JSX 写法 Javascript XML
                render() {
                    const username = "Cindy", age = 13;
                    const isShow = true;
                    const firstName = "Can";
                    const lastName = "Bear"
                    const obj = {
                        username: "Gee", age: 23
                    }
                    const arr = ['apple', 'banana', 'water'];
                    const users = [{
                        id: 1,
                        name: "Cindy",
                        age: 23
                    }, {
                        id: 2,
                        name: "Peter",
                        age: 13
                    }, {
                        id: 3,
                        name: "Ann",
                        age: 3
                    }]
                    const img = {
                        src: "https://img0.baidu.com/it/u=1023612170,1481600981&fm=26&fmt=auto",
                        title: "Cat"
                    }
                    const title = "XKJH KDBJHSBD S dsds"
                    const msg = [{
                        id: 1,
                        name: "xxxsdsdsds"
                    }, {
                        id: 2,
                        name: "fdferefe"
                    }, {
                        id: 3,
                        name: "dfggff"
                    }]

                    const attr = {
                        src: "https://img0.baidu.com/it/u=383573248,1121074447&fm=26&fmt=auto",
                        title: "It is a cat.",
                        alt: "cat"
                    }

                    const isButton = true

                    const pStyle = {
                        fontSize: "24px",
                        color: "orange"
                    }

                    return (
                        <div>
                            {/* 字符串 数字  */}
                            <div>{username}</div>
                            <div>{age}</div>
                            ----------

                            {/* 布尔值 null、undefined*/}
                            {/* 需要转换成字符串才会显示; 否则不显示 */}
                            <div>{'' + isShow}</div>
                            <div>{'' + null}</div>
                            ----------

                            {/* 运算符表达式 */}
                            <div>{firstName + " " + lastName}</div>
                            ----------

                            {/* 三元运算表达式 */}
                            <h2>{isShow ? 'Cat"s secrect' : 'House'}</h2>
                            <div>{!isButton ? <button>button</button> : <div>DIV</div>}</div>
                            ----------

                            {/* 对象 */}
                            <div>Username: {obj.username}</div>
                            <div>age: {obj.age}</div>
                            ----------

                            {/* 循环数组 */}
                            <ul>
                                {
                                    users.map(item => {
                                        return <li key={item.id}>name:{item.name};age: {item.age}</li>
                                    })
                                }
                            </ul>
                            ----------

                            {/* 标签属性直接使用{}包裹 */}
                            <img src={img.src} title={img.title} />
                            ----------

                            {/* 函数调用 */}
                            <h3>{this.getFullName()}</h3>
                            <div>{this.article()}</div>
                            <div>number: {this.getNum(12)}</div>
                            ----------

                            {/* 事件绑定函数*/}
                            <button onClick={this.printSth}>print</button>
                            <input onChange={this.onChange} placeholder="请输入" />
                            {/* 如果事件要传参,要在外面加一层箭头函数 或者使用 bind,否则执行不了 */}
                            <button onClick={() => { this.printVal(1) }}>get 1</button>
                            <button onClick={() => { this.printVal(10) }}>get 10</button>
                            <button onClick={this.printVal.bind(this, 21)}>get 21</button>
                            <button onClick={this.printVal.bind(this, 20)}>get 20</button>
                            ----------

                            {/* 标签属性使用扩展运算符 */}
                            <img {...attr} />
                            ----------

                            {/* 样式 */}
                            {/* 内联样式 */}
                            <p style={{ fontSize: "18px", color: "pink" }}>Paragraph</p>
                            <p style={ pStyle }>Paragraph</p>
                            {/* className */}
                            <p className='para'>Paragraph</p>
                            <p className='para active'>Paragraph</p>
                            {/* 还有使用classnames包的 */}
                        </div>
                    )
                }

                getFullName() {
                    return this.state.firstname + " " + this.state.lastname;
                }

                printSth() {
                    console.log('printing ... ');
                }

                article() {
                    return (
                        <div>
                            <h1>Heading</h1>
                            <p>paragraph 1</p>
                            <p>paragraph 2</p>
                            <p>paragraph 3</p>
                        </div>
                    )
                }

                getNum(val) {
                    return (
                        <b>{val}</b>
                    )
                }

                onChange(val) {
                    console.log('val', val);
                }

                printVal(val) {
                    console.log('val', val);
                }
            }
            ReactDOM.render(<App />, document.getElementById("app"));
        </script>
    </body>
</lang>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值