React JSX
React 使用 JSX 來(lái)替代常規(guī)的 JavaScript。
JSX 是一個(gè)看起來(lái)很像 XML 的 JavaScript 語(yǔ)法擴(kuò)展。
我們不需要一定使用 JSX,但它有以下優(yōu)點(diǎn):
JSX 執(zhí)行更快,因?yàn)樗诰幾g為 JavaScript 代碼后進(jìn)行了優(yōu)化。
它是類型安全的,在編譯過(guò)程中就能發(fā)現(xiàn)錯(cuò)誤。
使用 JSX 編寫(xiě)模板更加簡(jiǎn)單快速。
使用 JSX
JSX 看起來(lái)類似 HTML ,我們可以看下實(shí)例:
ReactDOM.render( <h1>Hello, world!</h1>, document.getElementById('example') );
我們可以在以上代碼中嵌套多個(gè) HTML 標(biāo)簽,需要使用一個(gè) 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">這是一個(gè)很不錯(cuò)的 JavaScript 庫(kù)!</p> </div> , document.getElementById('example') ); </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
獨(dú)立文件
你的 React JSX 代碼可以放在一個(gè)獨(dú)立文件上,例如我們創(chuàng)建一個(gè) 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á)式寫(xiě)在花括號(hào) {} 中。實(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 語(yǔ)句,單可以使用 conditional (三元運(yùn)算) 表達(dá)式來(lái)替代。以下實(shí)例中如果變量 i 等于 1 瀏覽器將輸出 true, 如果修改 i 的值,則會(huì)輸出 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 語(yǔ)法來(lái)設(shè)置內(nèi)聯(lián)樣式. React 會(huì)在指定元素?cái)?shù)字后自動(dòng)添加 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í)例
注釋
注釋需要寫(xiě)在花括號(hào)中,實(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ù)組會(huì)自動(dòng)展開(kāi)所有成員:
實(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 里使用小寫(xiě)字母的標(biāo)簽名。
var myDivElement = <div className="foo" />; ReactDOM.render(myDivElement, document.getElementById('example'));
要渲染 React 組件,只需創(chuàng)建一個(gè)大寫(xiě)字母開(kāi)頭的本地變量。
var MyComponent = React.createClass({/*...*/}); var myElement = <MyComponent someProperty={true} />; ReactDOM.render(myElement, document.getElementById('example'));
React 的 JSX 使用大、小寫(xiě)的約定來(lái)區(qū)分本地組件的類和 HTML 標(biāo)簽。
注意:
由于 JSX 就是 JavaScript,一些標(biāo)識(shí)符像
class
和for
不建議作為 XML 屬性名。作為替代,React DOM 使用className
和htmlFor
來(lái)做對(duì)應(yīng)的屬性。