- ReactJS by Example:Building Modern Web Applications with React
- Vipul A M Prathamesh Sonpatki
- 247字
- 2021-07-09 19:36:59
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'));
- Docker進階與實戰
- Java Web應用開發技術與案例教程(第2版)
- C++程序設計基礎教程
- TypeScript項目開發實戰
- Linux Device Drivers Development
- HTML5從入門到精通 (第2版)
- Windows Phone 7.5:Building Location-aware Applications
- 微服務從小白到專家:Spring Cloud和Kubernetes實戰
- CoffeeScript Application Development Cookbook
- Laravel Application Development Blueprints
- Python自然語言理解:自然語言理解系統開發與應用實戰
- Python計算機視覺和自然語言處理
- Training Systems Using Python Statistical Modeling
- PowerDesigner 16 從入門到精通
- RESTful Web API Design with Node.js