I wanted to remove a row from the table with the function 'deleteRow(btn)' when pressing the button, but I get this error 'Cannot read properties of undefined (reading 'parentNode')'. What could I add or correct to successively drop a row from a table?
App.js
class App extends React.Component{
constructor(props) {
super(props);
this.state = {
fotos: [],
restaurantes:[],
}
}
deleteRow=(btn)=> {
var row = btn.parentNode.parentNode;
row.parentNode.removeChild(row);
}
render(){
const { fotos, restaurantes } = this.state;
<div className="container">
<Tabela dadosFotos={fotos} restaurante={this.deleteRow} />
</div>
}
Tabela.js
import React from "react";
const CorpoTabela = (props) => {
const rows = props.dadosDasFotos.map((row) => {
return(
<tr key={row.idFoto}>
<td>{row.nomeRestaurante}</td>
<td>
<button className="btn btn-outline-danger"
onClick={()=>props.restauranteAremover(row.idFoto)}>
Delete restaurante
</button>
</td>
</tr>
)
})
return(<tbody>{rows}</tbody>)
}
class Tabela extends React.Component{
render(){
const { dadosFotos, restaurante }=this.props
return(
<table className="table table-striped">
<CorpoTabela dadosDasFotos={dadosFotos} restauranteAremover={restaurante}/>
</table>
)
}
}
fotosarray in state? You need to update the state so React triggers a rerender. Just deleting DOMNodes won't trigger your UI to rerender an updated view.