英文:
Can't add rows to table in react
问题
I have a react component. In a useEffect I'm trying to create a new row and add the row to the table. I tried using insertRow
, but that didn't work either. The element does exist and I am able to add a mousedown
listener to it.
const createRow = () => {
const row = document.createElement("tr");
for (let j = 0; j < 3; j++) {
const td = document.createElement("td");
row.appendChild(td);
}
return row;
};
useEffect(() => {
if (id !== "-1") {
const element = document.getElementById(id);
element?.addEventListener("mousedown", () => {
console.log("here");
});
const row = createRow();
element?.append(row); // also tried appendChild
// yes there should be a removeEventListener, but this is just to debug
}
}, [id]);
(Note: The code portion is provided in both English and Chinese, as requested.)
英文:
I have a react component. In a useEffect I'm trying to create a new row and add the row to the table. I tried using insertRow
, but that didn't work either. The element does exist and I am able to add a mousedown
listener to it.
<!-- language: lang-js -->
const createRow = () => {
const row = document.createElement("tr");
for (let j = 0; j < 3; j++) {
const td = document.createElement("td");
row.appendChild(td);
}
return row;
};
<!-- end snippet -->
<!-- language: lang-js -->
useEffect(() => {
if (id !== "-1") {
const element = document.getElementById(id);
element?.addEventListener("mousedown", () => {
console.log("here");
});
const row = createRow();
element?.append(row); // also tried appendChild
// yes there should be a removeEventListener, but this is just to debug
}
}, [id]);
<!-- end snippet -->
答案1
得分: 2
Your createRow
函数正常工作,问题在于你创建了三个 td
并将它们附加到创建的 tr
,但这些 td
是空的,所以函数返回如下内容:
<tr>
<td></td>
<td></td>
<td></td>
</tr>
这是一个具有三个空表格数据的表格行,这就是为什么无法观察到它的原因,所以根据相同的逻辑,你必须向你的 td
添加文本:
const createRow = () => {
const row = document.createElement("tr");
for (let j = 0; j < 3; j++) {
const td = document.createElement("td");
// 添加以下两行
const content = document.createTextNode("Test");
td.appendChild(content);
row.appendChild(td);
}
return row;
};
这将返回:
<tr>
<td>Test</td>
<td>Test</td>
<td>Test</td>
</tr>
注意: 这是纯JavaScript,如果你正在使用React,建议使用refs来操作DOM元素,而不是通过ID获取它们。
英文:
Your createRow
function is working fine the problem is that you are creating three td
and appending them to the created tr
but those td
are empty so the function is returning this :
<tr>
<td></td>
<td></td>
<td></td>
</tr>
It is a table row with three empty table data, that's why it can't be observed, so with the same logic you have to append text to your td
:
const createRow = () => {
const row = document.createElement("tr");
for (let j = 0; j < 3; j++) {
const td = document.createElement("td");
// add these two lines
const content = document.createTextNode("Test");
td.appendChild(content);
row.appendChild(td);
}
return row;
};
This will retun:
<tr>
<td>Test</td>
<td>Test</td>
<td>Test</td>
</tr>
Note: This is vanilla Javascript, if you are using react, it is recommanded to use refs to manipulate DOM elements instead of getting them by id
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论