我有一个react 组件.在useEffect中,我try 创建一个新行并将该行添加到表中.我试着用insertRow
,但也不起作用.该元素确实存在,并且我能够向其添加mousedown
侦听器.
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]);