React筆記01-JSX

來源:互聯網
上載者:User

標籤:組件   .com   element   doc   label   const   nbsp   new   ide   

0.對於學習React,我們先來熟悉下JSX的文法,

下面的這些文法僅用於構建一個組件的標籤模組,定義完成之後如果需要做示範,請附加以下代碼:

ReactDOM.render(    element1,    document.body);

即可完成標籤的示範。

1.普通的標籤
const element1 = <div>Hello JSX</div>;
2.帶子標籤的標籤
const element2 = (    <div>        <h1>Hello Child1</h1>        <span>Hello 2</span>    </div>);
3.1帶變數的標籤
const user = {    name: ‘qiumohanyu‘,    age: 27,    hobby: ‘nothing‘,    website: ‘www.baidu.com‘};const hasNav = true;const element3_1 = (    <div>        <h1>{user.name}</h1>        <span>{user.age}</span>        <span>{user.hobby}</span>        <a src={user.website}>首頁</a>        <div> {hasNav ? <nav>Nav</nav> : null}</div>    </div>);
3.2 內建函數的標籤
function getItem() {    return ‘from function‘;}const element3_2 = <div>{getItem()}</div>;
4.1 帶類名的標籤 class=>className
const type = true;const element4_1 = (    <div>        <div className="mycls">帶class的標籤</div>        <span className={type?‘class1‘:‘class2‘}>帶判斷邏輯的類名</span>    </div>);
4.2 帶tabindex的標籤 tabindex=>tabIndex
const element4_2 = <div tabIndex="0">帶tabindex的標籤</div>;
4.3 帶for屬性的label
const element4_3 = <label htmlFor="input1">User Name</label>;
5.1 屬性
const MyComponent = React.createClass(    {        render: function() {            return <div>My Component</div>        }    });var x = "foo-value";var y = "bar-value";const element5_1 = <MyComponent foo={x} bar={y}/>;
5.2 屬性延伸
var props = {};props.foo = x;props.bar = y;const element5_2 = <MyComponent {...props} />;//需要覆蓋時const element5_3 = <MyComponent {...props} foo={‘new-value‘}/>;
6 style屬性
var mystyles = {    color: ‘red‘,    border: ‘1px solid #000‘};const element6 = <div style={mystyles}></div>;
7 HTML轉義
var innerHtml = ‘<span> my span label</span>‘;var toDiv = { __html: innerHtml };const element7 = (    <div dangerouslySetInnerHTML={toDiv}></div>);
8 自訂HTML 屬性
const element8=(    <div data-id="can render" ride="can not be render"></div>);

 

React筆記01-JSX

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.