JSX is NOT an HTML
nor a string
.
JSX is a syntax extension to Javascript.
React embraces the fact that rendering logic is inherently coupled with other UI logic.
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.
React doesn’t require using JSX, but it's helpful.
JSX allows JS to be expressed inside.
JS allows JSX to be expressed inside when using curly bracket:
// JSX inside JS const name = 'Josh Perez'; const element = <h1>Hello, {name}</h1>; // JS inside JSX const element = ( <h1> Hello, {formatName(user)}! </h1> );
After compilation, JSX expressions become regular JS function call, evaluating to JS objects.
Meaning - you can use JSX inside of if
statements and for
loops.
function getGreeting(user) { if (user) { return <h1>Hello, {formatName(user)}!</h1>; } return <h1>Hello, Stranger.</h1>; }
JSX is intended to be used by various preprocessors (transpilers) to transform these tokens into standard ECMAScript.
Unlike browser DOM elements, React elements are plain objects, and are cheap to create. React DOM takes care of updating the DOM to match the React elements.