JSX
Using React.createElement()
is very verbose, so most of the time we use JSX, a syntax extension to JavaScript. React brings HTML and JS together into an unholy creation called JSX. JSX allows us to write code that looks like HTML inside of our JavaScript but is eventually compiled down to JavaScript objects.
JSX is not valid JavaScript - but it is transpiled/compiled into valid JavaScript.
JSX Rules & Conventions
JSX can only render one top level element but that element can contain numerous children.
All tags need to be closed. Self-closing tags like
<img>
need to be written as<img />
.Any JavaScript within JSX must be enclosed in curly braces
{}
.The keyword class is reserved so the
class
attribute must be renamed toclassName
. There are other differences between HTML attributes and JSX. We'll get to those when we need them, butclassName
is the first and most used one we'll encounter.
References
Last updated
Was this helpful?