How use React JSX tag: Confused by this statement,when I use , it’s a compilation error

because your JSX variable should have one element that wrap everything, so your code is not valid.

before, the div#1 was around all the other elements, so it was fine. Now you added div#2 outside, so it’s not fine anymore.
You need to write something like below, using the fragment, or using a proper element. That’s why earlier I told you to put div#2 inside div#1 to avoid the error, but as you don’t want to do that, this is an other alternative.

const JSX = (
<>
  <div id="1">
    <h2>Welcome to React!</h2> <br/>
    <p>Be sure to close all tags!</p>
    <hr />
  </div>

  <div id="2" />
</>
);
1 Like