官术网_书友最值得收藏!

JavaScript expressions

"Shawn, let's discuss a bit about how we have rendered the Rows and Headings tag."

render: function() {
    var headings = this.props.headings.map(function(name) {
      return(<Heading heading = {name}/>);
    });

   return <tr>{headings}</tr>;
  }

"We are rendering {headings}, which is a list of React components, directly by adding them in curly braces as children of the <tr> tag. These expressions that are used to specify the child components are called child expressions."

"There is another category of expressions called as JavaScript expressions. These are simple expressions used for passing props or evaluating some JavaScript code that can be used as an attribute value."

// Passing props as expressions
ReactDOM.render(<App headings = {['When', 'Who', 'Description']} 
                  data = {data} />, 
                document.getElementById('container'));

// Evaluating expressions
ReactDOM.render(<App headings = {['When', 'Who', 'Description']} 
                  data = {data.length > 0 ? data : ''} />, 
                document.getElementById('container'));

"Anything that is present in curly braces is evaluated by JSX. It works for both children expressions as well as JavaScript expressions," added Mike.

"Thanks for the detailed explanation. I have a query though. Is there any way to write comments in the JSX code? I mean, we may not need it all the time, but knowing how to add comments might be handy," Shawn asked.

"Remember the curly braces rule. Comments are simply just JavaScript expressions. When we are in a child element, just wrap the comments in the curly braces."

render: function() {
    return(<th> 
             {/* This is a comment */}
             {this.props.heading}
           </th>);
  }

"You can also add comments in a JSX tag. There is no need to wrap them in curly braces in this case," Mike added.

ReactDOM.render(<App 
                  /* Multi
                     Line 
                     Comment
                  */
                  headings = {headings} 
                  changeSets = {data} />, 
                document.getElementById('container'));
主站蜘蛛池模板: 威信县| 大田县| 堆龙德庆县| 山东省| 和龙市| 连江县| 沅江市| 收藏| 巩留县| 大洼县| 湛江市| 五台县| 东乡县| 阳高县| 航空| 宝应县| 迭部县| 尼勒克县| 高平市| 阜新市| 芜湖县| 桦南县| 常德市| 梧州市| 合阳县| 竹北市| 花垣县| 辽阳市| 志丹县| 大新县| 渝北区| 天津市| 江门市| 楚雄市| 台湾省| 武鸣县| 镇远县| 大港区| 庆元县| 鱼台县| 砚山县|