当前位置:   article > 正文

JSX使用、写法_jsx写法

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205

  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/494391
推荐阅读
相关标签
  

闽ICP备14008679号