JSX Rules
General rules
JSX essentially works like HTML, but allows a “JavaScript-Mode” by using
{}(for text or attributes)Inside
{}JavaScript expressions are allowed. Example: reference variable, create arrays or objects, [].map(), ternary operator.Statements are not allowed (e.g. if/else, for, switch)
JSX produces a JavaScript expression (for example a JSX HTML element is translated into a
React.create()function call, which is an expression)
Other pieces of JSX can be placed inside
{}JSX can be written anywhere inside a component (e.g. inside if/else, assign JSX to variables, pass JSX into functions)
a piece of JSX can only have one root element. If more are needed, use
<React.Fragment>(or use short<>):<> <OneChild /> <AnotherChild /> </>
Differences between JSX and HTML
classNameinstead of HTML’sclasshtmlForinstead of HTML’sforEvery tag needs to be closed. Examples:
<img /> or ``<br />All event handlers and other properties need to be camelCased. Examples:
onClickoronMouseOverException:
aria-*anddata-*are written with dashes like in HTMLCSS inline styles are written like this: {{<style>}} (to reference a variable and then an object)
CSS property names are also camelCased
Comments need to be in
{}(because they are JS)