国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

React JSX

React 使用 JSX 來替代常規(guī)的 JavaScript。

JSX 是一個看起來很像 XML 的 JavaScript 語法擴(kuò)展。

我們不需要一定使用 JSX,但它有以下優(yōu)點(diǎn):

  • JSX 執(zhí)行更快,因為它在編譯為 JavaScript 代碼后進(jìn)行了優(yōu)化。

  • 它是類型安全的,在編譯過程中就能發(fā)現(xiàn)錯誤。

  • 使用 JSX 編寫模板更加簡單快速。


使用 JSX

JSX 看起來類似 HTML ,我們可以看下實(shí)例:

ReactDOM.render(
	<h1>Hello, world!</h1>,
	document.getElementById('example')
);

我們可以在以上代碼中嵌套多個 HTML 標(biāo)簽,需要使用一個 div 元素包裹它,實(shí)例中的 p 元素添加了自定義屬性 data-myattribute,添加自定義屬性需要使用 data- 前綴。

實(shí)例

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>php中文網(wǎng) React 實(shí)例</title>
    <script src="http://static.php.cn/assets/react/react-0.14.7/build/react.min.js"></script>
    <script src="http://static.php.cn/assets/react/react-0.14.7/build/react-dom.min.js"></script>
    <script src="http://static.php.cn/assets/react/browser.min.js"></script>
  </head>
  <body>
    <div id="example"></div>
    <script type="text/babel">
      ReactDOM.render(
      	<div>
      	<h1>php中文網(wǎng)</h1>
      	<h2>歡迎學(xué)習(xí) React</h2>
        <p data-myattribute = "somevalue">這是一個很不錯的 JavaScript 庫!</p>
        </div>
      	,
      	document.getElementById('example')
      );
    </script>
  </body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

獨(dú)立文件

你的 React JSX 代碼可以放在一個獨(dú)立文件上,例如我們創(chuàng)建一個 helloworld_react.js 文件,代碼如下:

ReactDOM.render(
  <h1>Hello, world!</h1>,
  document.getElementById('example')
);

然后在 HTML 文件中引入該 JS 文件:

實(shí)例

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello React!</title>
    <script src="http://static.php.cn/assets/react/react-0.14.7/build/react.min.js"></script>
    <script src="http://static.php.cn/assets/react/react-0.14.7/build/react-dom.min.js"></script>
    <script src="http://static.php.cn/assets/react/browser.min.js"></script>
  </head>
  <body>
    <div id="example"></div>
  <script type="text/babel" src="helloworld_react.js"></script>
  </body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


JavaScript 表達(dá)式

我們可以在 JSX 中使用  JavaScript 表達(dá)式。表達(dá)式寫在花括號 {} 中。實(shí)例如下:

實(shí)例

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>php中文網(wǎng) React 實(shí)例</title>
    <script src="http://static.php.cn/assets/react/react-0.14.7/build/react.min.js"></script>
    <script src="http://static.php.cn/assets/react/react-0.14.7/build/react-dom.min.js"></script>
    <script src="http://static.php.cn/assets/react/browser.min.js"></script>
  </head>
  <body>
    <div id="example"></div>
    <script type="text/babel">
      ReactDOM.render(
      	<div>
      	  <h1>{1+1}</h1>
        </div>
      	,
      	document.getElementById('example')
      );
    </script>
  </body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

在 JSX 中不能使用 if else 語句,單可以使用 conditional (三元運(yùn)算) 表達(dá)式來替代。以下實(shí)例中如果變量 i 等于 1 瀏覽器將輸出 true, 如果修改 i 的值,則會輸出 false.

實(shí)例

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>php中文網(wǎng) React 實(shí)例</title>
    <script src="http://static.php.cn/assets/react/react-0.14.7/build/react.min.js"></script>
    <script src="http://static.php.cn/assets/react/react-0.14.7/build/react-dom.min.js"></script>
    <script src="http://static.php.cn/assets/react/browser.min.js"></script>
  </head>
  <body>
    <div id="example"></div>
    <script type="text/babel">
	  var i = 1;
      ReactDOM.render(
      	<div>
      	  <h1>{i == 1 ? 'True!' : 'False'}</h1>
        </div>
      	,
      	document.getElementById('example')
      );
    </script>
  </body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


樣式

React 推薦使用內(nèi)聯(lián)樣式。我們可以使用 camelCase 語法來設(shè)置內(nèi)聯(lián)樣式. React 會在指定元素數(shù)字后自動添加 px 。以下實(shí)例演示了為 h1 元素添加 myStyle 內(nèi)聯(lián)樣式:

實(shí)例

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>php中文網(wǎng) React 實(shí)例</title>
    <script src="http://static.php.cn/assets/react/react-0.14.7/build/react.min.js"></script>
    <script src="http://static.php.cn/assets/react/react-0.14.7/build/react-dom.min.js"></script>
    <script src="http://static.php.cn/assets/react/browser.min.js"></script>
  </head>
  <body>
    <div id="example"></div>
    <script type="text/babel">
      var myStyle = {
         fontSize: 100,
         color: '#FF0000'
      };
      ReactDOM.render(
      	<h1 style = {myStyle}>php中文網(wǎng)</h1>,
      	document.getElementById('example')
      );
    </script>
  </body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


注釋

注釋需要寫在花括號中,實(shí)例如下:

實(shí)例

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>php中文網(wǎng) React 實(shí)例</title>
    <script src="http://static.php.cn/assets/react/react-0.14.7/build/react.min.js"></script>
    <script src="http://static.php.cn/assets/react/react-0.14.7/build/react-dom.min.js"></script>
    <script src="http://static.php.cn/assets/react/browser.min.js"></script>
  </head>
  <body>
    <div id="example"></div>
    <script type="text/babel">
      ReactDOM.render(
      	<div>
            <h1>php中文網(wǎng)</h1>
            {/*注釋...*/}
         </div>,
      	document.getElementById('example')
      );
    </script>
  </body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


數(shù)組

JSX 允許在模板中插入數(shù)組,數(shù)組會自動展開所有成員:

實(shí)例

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>php中文網(wǎng) React 實(shí)例</title>
    <script src="http://static.php.cn/assets/react/react-0.14.7/build/react.min.js"></script>
    <script src="http://static.php.cn/assets/react/react-0.14.7/build/react-dom.min.js"></script>
    <script src="http://static.php.cn/assets/react/browser.min.js"></script>
  </head>
  <body>
    <div id="example"></div>
    <script type="text/babel">
      var arr = [
        <h1>php中文網(wǎng)</h1>,
        <h2> php中文網(wǎng)</h2>,
      ];
      ReactDOM.render(
        <div>{arr}</div>,
        document.getElementById('example')
      );
    </script>
  </body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


HTML 標(biāo)簽 vs. React 組件

React 可以渲染 HTML 標(biāo)簽 (strings) 或 React 組件 (classes)。

要渲染 HTML 標(biāo)簽,只需在 JSX 里使用小寫字母的標(biāo)簽名。

var myDivElement = <div className="foo" />;
ReactDOM.render(myDivElement, document.getElementById('example'));

要渲染 React 組件,只需創(chuàng)建一個大寫字母開頭的本地變量。

var MyComponent = React.createClass({/*...*/});
var myElement = <MyComponent someProperty={true} />;
ReactDOM.render(myElement, document.getElementById('example'));

React 的 JSX 使用大、小寫的約定來區(qū)分本地組件的類和 HTML 標(biāo)簽。

注意:

由于 JSX 就是 JavaScript,一些標(biāo)識符像 classfor 不建議作為 XML 屬性名。作為替代,React DOM 使用 classNamehtmlFor 來做對應(yīng)的屬性。