79429959

Date: 2025-02-11 12:32:26
Score: 0.5
Natty:
Report link
function TableComponent({ numRows }) {
 return (
 <tbody>
  {Array.from({ length: numRows }).map((_, index) => (
    <ObjectRow key={index} />
  ))}
</tbody>
);
}

function ObjectRow() {
 return (
  <tr>
   <td>Row Content</td>
  </tr>
);
}

export default TableComponent;

How It Works

Array.from({ length: numRows }) creates an array with numRows elements. .map() iterates over the array, returning an ObjectRow component for each element. The key prop is added to each row for efficient rendering and reconciliation by React.

Why map and Not for?

JSX expects expressions, not statements. for is a statement, whereas map() is an expression that directly returns an array of elements. This functional approach is the most idiomatic in React.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): How It
  • Low reputation (0.5):
Posted by: Ismail