Below is part of my render method in JSX - why does the semicolon after the })
throw an error? It's perfectly fine in normal JavaScript
<ul>
{
libraries.map(function (item) {
return <li>{item.name.toLowerCase()}</li>;
});
}
</ul>
It doesn't matter.
JSX may remind you of a template language, but it comes with the full power of JavaScript. JSX produces React “elements”. Instead of artificially separating technologies by putting markup and logic in separate files, React separates concerns with loosely coupled units called “components” that contain both.
JavaScript is an interpreted language, not a compiled language. A program such as C++ or Java needs to be compiled before it is run. The source code is passed through a program called a compiler, which translates it into bytecode that the machine understands and can execute.
JSX is not a requirement for using React. Using React without JSX is especially convenient when you don't want to set up compilation in your build environment. Each JSX element is just syntactic sugar for calling React.
It's because JSX {}
expressions are limited to a single expression.
<div>{2 + 2; 3 + 3}</div>
..will throw an error.
However you can solve this by having two {}
expressions
<div>{2 + 2}{3 + 3}</div>
There's no need for semi-colons if there will only be a single expression.
If you want to get all hacky about it, you can use the lesser known comma operator in expressions to do some local work, before returning the last expression separated by commas:
let x
...
<div>{x = 20, x}</div>
Which will display <div>20</div>
You might not need semi-colons anywhere.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With