Inline conditional JSX – Expected corresponding JSX closing tag for <div>

huangapple go评论71阅读模式
英文:

Inline conditional JSX - Expected corresponding JSX closing tag for <div>

问题

Here is the translated code without the non-translatable parts:

const gridSquares = squares.map((reference, square) => {
  return (
    <React.Fragment>
      {square % 3 === 0 && square % 3 !== squares.length && (
        <div className="board-row">
      )}
      <Square
        key={square}
        value={squares[square]}
        onSquareClick={() => handleClick(square)}
      />
    </React.Fragment>
  );
});

return (
  <div>
    <div className="status">{status}</div>
    {gridSquares}
  </div>
);

It seems like there is a syntax error in your code related to the closing tags, which I have translated without changing. You may need to inspect your code for the specific error.

英文:

Is it possible to have inline conditional jsx without the closing tag? I need to insert &lt;div className=&quot;board-row&quot;&gt; every 3rd square.

const gridSquares = squares.map((reference, square) =&gt; {
  return (
    &lt;React.Fragment&gt;
      {square % 3 == 0 &amp;&amp; square % 3 !== squares.length &amp;&amp; (
        &lt;div className=&quot;board-row&quot;&gt;
      )}
      &lt;Square
        key={square}
        value={squares[square]}
        onSquareClick={() =&gt; handleClick(square)}
      /&gt;
    &lt;/React.Fragment&gt;
  );
});

return (
  &lt;div&gt;
    &lt;div className=&quot;status&quot;&gt;{status}&lt;/div&gt;
    {gridSquares}
  &lt;/div&gt;
);

Error:

./src/App.js
Syntax error: C:/Code/Tutorial/src/App.js: Expected corresponding JSX closing tag for &lt;div&gt; (57:4)

  55 |       &lt;Square key={square} value={squares[square]} onSquareClick={() =&gt; handleClick(square)} /&gt;
  56 | 
&gt; 57 |     &lt;/React.Fragment&gt;
     |     ^
  58 |     
  59 |   });

答案1

得分: 1

JSX不像HTML模板。每个元素都会被编译为JS,并且必须在结构上有效。

假设你想要创建一个每行有3个元素的网格,你可以使用CSS grid来简化,而不必担心标记。

return (
  <div>
    <div className="status">{status}</div>
    <div className="board-grid">
      {squares.map((square, index) => (
        <Square
          key={index}
          value={square}
          onSquareClick={() => handleClick(square)}
        />
      ))}
    </div>
  </div>
);
.board-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr); /* 每行3个方块 */
}

Inline conditional JSX – Expected corresponding JSX closing tag for <div>

英文:

JSX isn't like an HTML template. Each element is compiled to JS and must be structurally valid.

Assuming you're trying to make some sort of grid with 3 elements per row, you could use a CSS grid for simplicity without worrying about markup

return (
  &lt;div&gt;
    &lt;div className=&quot;status&quot;&gt;{status}&lt;/div&gt;
    &lt;div className=&quot;board-grid&quot;&gt;
      {squares.map((square, index) =&gt; (
        &lt;Square
          key={index}
          value={square}
          onSquareClick={() =&gt; handleClick(square)}
        /&gt;
      ))}
    &lt;/div&gt;
  &lt;/div&gt;
);
.board-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr); /* 3 squares per row */
}

Inline conditional JSX – Expected corresponding JSX closing tag for <div>

huangapple
  • 本文由 发表于 2023年4月11日 06:49:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定