ReactDOM.render(
<h1>Hello world!</h1>,
document.getElementById('root')
);
const element = <h1>Hello, world!</h1>;
In the example below, we declare a variable called name and then use it inside JSX by wrapping it in { } curly braces
const name = 'Josh Perez';
const element = <h1>Hello, {name}</h1>;
ReactDOM.render(
element,
document.getElementById('root')
);
You can put any valid JavaScript expression inside
{ } :the curly braces
in JSX. For example, 2 + 2, user.firstName, or formatName(user) are all valid JavaScript expressions.
We split JSX over multiple lines for readability. While it isn’t required, when doing this, we also recommend wrapping it in
( ) parentheses
to avoid the pitfalls(함정) of automatic semicolon insertion.
function formatName(user) {
return user.firstName + ' ' + user.lastName;
}
const user = {
firstName: 'Harper',
lastName: 'Perez'
};
const element = (
<h1>
Hello, {formatName(user)}!
</h1>
);
ReactDOM.render(
element,
document.getElementById('root')
);
you can use JSX inside of if statements and for loops, assign it to variables, accept it as arguments, and return it from functions:
function getGreeting(user) {
if (user) {
return <h1>Hello, {formatName(user)}!</h1>;
}
return <h1>Hello, Stranger.</h1>;
}
React Official Docs - Introducing JSX :
https://reactjs.org/docs/introducing-jsx.html