Multiple JSX element always have to be closed by one parent element.
<div>
<h1>Hello</h1>
</div>
<div>
<h2>World</h2>
</div>
Codes above need to be wrapped with one elemnt like the codes below.
<div>
<div>
<h1>Hello</h1>
</div>
<div>
<h2>World</h2>
</div>
</div>
When we want to add a class name into a JSX element, we have to use className
instead of class
because in React, it does not translated as HTML class name but it translate as JavaScript Class.
<div class="hello">Hello world!</div> // THIS IS WRONG WAY OF ADDING CLASS NAME IN JSX!!!!
<div className="hello">Hello world!</div> // THIS IS RIGHT WAY OF ADDING CLASS NAME IN JSX!!!!
If we want to use JavaScript in JSX, we need to wrap that JavaScript Code with curly bracket like the codes below.
function App() {
const name = 'Minwoo Gwak'
return (
<div>
Hello, {name} // using curly bracket for adding JavaScript code in JSX
</div>
);
}
If we use the component name starts with lower case, then React interpret it as regular HTML element.
function greeting() { // It is not starting with capital letter
const name = 'Minwoo Gwak'
return (
<div>
Hello, {name} // using curly bracket for adding JavaScript code in JSX
</div>
);
}
This codes need to be changed like below:
function Greeting() { // It starts with a capital letter!
const name = 'Minwoo Gwak'
return (
<div>
Hello, {name} // using curly bracket for adding JavaScript code in JSX
</div>
);
}
We need to use ternary expression instead of if
statement
function Greeting() {
const name = 'Minwoo Gwak'
return (
<div>
Hello, {name === 'Minwoo Gwak' ? 'admin' : 'customer'}
</div>
);
}