zoukankan      html  css  js  c++  java
  • React(JSX语法)-----JSX基本语法

    JSX------HTML tags vs React Components:

      1.To render a html tag,just use lower-case tag names in JSX; 

    var myDivElement = <div className="foo" />;
    React.render(myDivElement, document.body);
    

      2.To render a React Component,just create a local variable that starts with an upper-case latter;

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

      

    JSX-------The Transform

      React JSX transforms from an XML-like syntax into native JS xml elements,attributes and children are transformed into arguments to React.createElement

    var Nav;
    // Input (JSX):
    var app = <Nav color="blue" />;
    // Output (JS):
    var app = React.createElement(Nav, {color:"blue"});
    

      

    var Nav, Profile;
    // Input (JSX):
    var app = <Nav color="blue"><Profile>click</Profile></Nav>;
    // Output (JS):
    var app = React.createElement(
      Nav,
      {color:"blue"},
      React.createElement(Profile, null, "click")
    );
    

      

    Javascript Expressions

      1.Attribute Expressions:To use a JS expression as an attribute value,wrap the expression in a pair of curly braces({})instead of quotes("").

    // Input (JSX):
    var person = <Person name={window.isLoggedIn ? window.name : ''} />;
    // Output (JS):
    var person = React.createElement(
      Person,
      {name: window.isLoggedIn ? window.name : ''}
    );
    

      2.Child Expressions:Likewise,JS expressions may be used to express children:

    / Input (JSX):
    var content = <Container>{window.isLoggedIn ? <Nav /> : <Login />}</Container>;
    // Output (JS):
    var content = React.createElement(
      Container,
      null,
      window.isLoggedIn ? React.createElement(Nav) : React.createElement(Login)
    );
    

      3.Comments:it's easy to add comments within youe JSX,they are just JS expressions. you just need to bo careful to put {} around the comments when you are within the children sction of a tag.

    var content = (
      <Nav>
        {/* child comment, put {} around */}
        <Person
          /* multi
             line
             comment */
          name={window.isLoggedIn ? window.name : ''} // end of line comment
        />
      </Nav>
    );
    

      

  • 相关阅读:
    批量清理java源码的target目录
    前端移动node_modules到其他位置
    oracle物化视图和视图的数据不一致
    百词斩英语单词素材提取、听力练习
    2048自动游戏AI, 最高可以玩出一二十个2048
    switcheroo: Alt+Tab的替代工具、窗口搜索
    为知笔记wiz.editor.md增强
    腾讯北极星 Polaris 试用
    [分布式] 分布式事务、seata
    Mysql查询所有的表名和查询表中所有的字段名
  • 原文地址:https://www.cnblogs.com/jodie-blog/p/5109009.html
Copyright © 2011-2022 走看看