I’m encountering an issue in my React application where I’m rendering a table with dynamic rows using components. However, the rows are being treated as separate elements instead of components within the table body.
Despite generating the rows dynamically using components, they’re not aligning properly with the table headers. I’ve tried various approaches, including encapsulating each row within its own element, but the issue persists.
- TableHead Component:
function TableHead({ rowCount }) {
return (
<div className='table-container'>
<table>
<thead>
<tr>
<th>S. No</th>
<th>Subject</th>
<th>Grade</th>
</tr>
</thead>
<tbody>
<tr><InitialRender count={rowCount} /></tr>
</tbody>
</table>
</div>
);
}
- InitialRender Component:
function InitialRender({ count }) {
const initialRows = [];
for (let i = 0; i < count; i++) {
initialRows.push(
<TableRow key={i + 1} count={i + 1} />
);
}
return initialRows;
}
- TableRow Component
function TableRow({ count }) {
return (
<tr>
<td>{count}</td>
<td><input type="text" placeholder='Enter subject' /></td>
<td>
<select name="" id="dropdown">
<option value="">Grades</option>
<option value="O">O</option>
<option value="A+">A+</option>
<option value="A">A</option>
<option value="B+">B+</option>
<option value="B">B</option>
<option value="AB">AB</option>
</select>
</td>
</tr>
);
}
- App Component:
function App() {
const [rowCount, setRowCount] = useState(6);
const handleAddRow = () => {
setRowCount(prevCount => prevCount + 1);
};
return (
<div className='form-container'>
<form action=''>
<TableHead rowCount={rowCount} />
<InitialRender count={rowCount} />
<div className='buttons'>
<AddRow onAddRow={handleAddRow} />
<Submit />
</div>
</form>
</div>
);
}
2
Answers
The
TableHead
component already has theInitialRender
as a child and you are calling it again fromApp
. You need to remove<InitialRender count={rowCount} />
fromApp
component. TheTableHead
will call it.(I have removed
<form>
from theApp
here in the snippet, you can add it back.)Some nitpicking:
First since you are using react you should divide the program in actual framgents.
Your TableRow is fine but your tableHead should either be renamed to table since you are making a table and not the header.
Your problem:
Both in TableHead and the App you are calling for the InitialRender, since you are already calling it on your TableHeader remove it from the app. The InitialRender you had in the app was not inside the table so that created your problem
If you do that it looks at it should table
More nitpicking:
You could also just remove completely your InitialRender using something like this on your TableHeader
Using the Array.from() making you a loop reducing you a function but yours still good.