I’m a beginner to react and I’ve been trying to understand the basis. I’m confused with the exact meaning of the error: Adjacent JSX elements must be wrapped in an enclosing tag. Here’s the code in which the error occurs.
function App() {
const name = "John";
const isNameShowing = false;
return (
<div className="App">
<header className="App-header">
<h1>
{isNameShowing ? (
test
) : (
<h1>test</h1>
<h2>There is no name</h2>
)}
</h1>
</header>
</div>
);
}
export default App;
I understand that the error part occurs here in this block
<h1>
{isNameShowing ? (
test
) : (
<h1>test</h1>
<h2>There is no name</h2>
)}
</h1>
The error says I’ve to put self enclosing tags or a parent div element. But isn’t it already enclosed within the main div element ?
Also isn’t the below part of the code already enclosed within the parent <h1>
element around it?
(
<h1>test</h1>
<h2>There is no name</h2>
)
Inspite of having <h1>
as a parent element and even if that doesn’t suffice, and the main div element being the parent element, the error still pops up. What exactly does being wrapped around the parent element mean to prevent this error ?