react 的 jsx 语法规则

标题jsx 语法规则

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

<head>
    <meta charset="UTF-8">
    <title>jsx语法规则</title>
    <style>
        .title {
            background-color: rgba(207, 19, 91, 0.589);
            width: 200px;
        }
    </style>
</head>

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

    <script type="text/javascript" src="../js/react.development.js"></script>
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <script type="text/babel">
        const myId = "TeSt2"
        const myData = "HeLlo World"
        const VDOM = (
        /* class="title" 报错:react-dom.development.js:500 Warning: Invalid DOM property `class`. Did you mean `className`?  in h2*/
        /* style="background-color:red" 报错:
        Uncaught Invariant Violation: The `style` prop expects a mapping from style properties to values, not a string. For example, style={{marginRight: spacing + 'em'}} when using JSX.
    in ...
        */
            // <h2 class="title" id={myId.toLowerCase()}>
            <div>
                <h2 className="title" id={myId.toLowerCase()}>
                    <span style={{ color: "white", fontSize: '30px' }}>{myData.toLocaleLowerCase()}</span>
                </h2 >
                <h2 className="title" id={myId.toUpperCase()}>
                    <span style={{ color: "white", fontSize: '30px' }}>{myData.toLocaleLowerCase()}</span>
                </h2 >
                <input type="text" />
                <good>123</good>
                <Good>123</Good>

            </div>
        )
        /* const VDOM = (
            <h2 id="test2">
                    <span>Hello World</span>
                </h2>
        ) */
        ReactDOM.render(VDOM, document.getElementById('test'))

        /*
            jsx 语法规则:
                1、定义虚拟 DOM 时,不要写引号;
                2、标签中混入 js 表达式时要用 {};
                3、样式的类名不能用“class”,要用“className”;
                4、内联样式要用 style={{key:value}} 的形式去写,如,style={{ color: "white", fontSize: '30px' }}
                5、只能有一个跟标签;
                6、标签必须闭合;
                7、标签首字母
                    (1)若小写字母开头,会将该标签转为 HTML 中同名元素,若html中无该标签对应的同名元素,则报错;
                    (2)若大写字母开头,react就会渲染对应的组件,若组件没有定义,则会报错(Good is not defined)
        */
    </script>
</body>

</html>

1、定义虚拟 DOM 时,不要写引号;
2、标签中混入 js 表达式时要用 {};
3、样式的类名不能用“class”,要用“className”;
4、内联样式要用 style={{key:value}} 的形式去写,如,style={{ color: “white”, fontSize: ‘30px’ }}
5、只能有一个跟标签;
6、标签必须闭合;
7、标签首字母
(1)若小写字母开头,会将该标签转为 HTML 中同名元素,若html中无该标签对应的同名元素,则报错;
(2)若大写字母开头,react就会渲染对应的组件,若组件没有定义,则会报错(Good is not defined)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值