I would like to use Axios to delete a book from my list. I use the URL of my database + the id, but I can't find the way to specify the id in the URL and it stays undefined.
It looks like this :
export default class ListBooks extends React.Component {
constructor(props) {
super(props);
this.state = { error: null, data: [] }
}
componentDidMount() {
Axios.get(process.env.REACT_APP_API_PATH_BOOKS)
.then(res => {
this.setState({ data: res.data });
})
.catch(errorThrown => {
this.setState({ error: errorThrown });
})
}
handleChange = event => {
this.setState({ number: event.target.value });
}
/**
* Use to delete a book by the id.
*/
handleDelete = () => {
const id = this.data.number
console.log(id);
Axios.delete(process.env.REACT_APP_API_PATH_BOOKS + id)
.then(res => {
console.log(res);
console.log(res.data);
let cible = document.getElementById("book-admin" + id);
cible.remove();
})
.catch(errorThrown => {
this.setState({ error: errorThrown });
})
}
render() {
const { data } = this.state;
return (
<div>
<Container>
{data.map(books =>
<div key={books.number}>
<ListGroup>
<ListGroup.Item disabled id={"book-admin" + data.number}>{books.name} {books.author}
</ListGroup.Item>
</ListGroup>
<Button variant="outline-warning" size="sm" className="btn-admin-change" id={data.number} onClick={this.props.handleUpdate}>Modifier</Button>
<Button variant="outline-danger" size="sm" className="btn-admin-change" onClick={this.handleDelete}>Supprimer</Button>
</div>
)}
</Container>
</div>
)
}
}
Each time I push Delete button I have this error : TypeError: Cannot read property 'number' of undefined.
Can someone tell my what I did wrong? Or what I need to had to have all informations needed? Thank you