Grammer

本文介绍了如何在React中混合使用JavaScript与HTML代码,通过具体的示例展示了React独特的语法特性,使得开发者能够更加灵活地组织代码。

Now let's study the grammer for react.

Usually, we write the js code and html code in a separate way. Just as below:
1108876-20170812162023554-1377906988.png
1108876-20170812162542867-348054499.png

But react changed the way. It makes easier to write js & html tags and you can mix them together without caring about anything unless you obey the rule below:

Each time when react met <,it will compile with html tags ; If react met { ,it will compile with js.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../js/bootstrap.css">
    <script src="../js/react.js"></script>
    <script src="../js/react-dom.js"></script>
    <script src="../js/browser.js"></script>
    <title>React02</title>
</head>
<body>
<div id="example"></div>
<script type="text/babel">
    var names = ['Alice', 'Emily', 'Kate'];

    ReactDOM.render(
            <div>
                {
                    names.map(function (name) {
                        return <div>Hello, {name}!</div>
                    })
                }
            </div>,
        document.getElementById('example')
    );
</script>
</body>
</html>

Open the browser, you will see:

1108876-20170812163405023-1389171889.png

Another example


var arr = [
  <h1>Hello world!</h1>,
  <h2>React is awesome</h2>,
];
ReactDOM.render(
  <div>{arr}</div>,
  document.getElementById('example')
);

Looks strange,but legal. It's just what react is.

1108876-20170812164016570-894042219.png

转载于:https://www.cnblogs.com/xiongwei2017/p/7350659.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值