I am trying to build a .net core web app with react. However I'm struggling with some API calls and function. This may be asked before but I could not find the solution for myself.
class Documents extends Component{
constructor(props){
super(props);
this.state = {
docs: []
};
this.deleteHandle = this.deleteHandle.bind(this);
}
componentDidMount(){
const url = 'api/Documents';
fetch(url)
.then((response) => {
return response.json();
})
.then((data) =>{
this.setState({
docs: data
});
})
.catch((error) => console.log(error));
}
renderDocuments(){
return this.state.docs.map((doc) => (
<Doc key={doc.id} doc = {doc}/>
));
}
deleteHandle(id) {
fetch('api/Documents/'+id, { method: 'DELETE' })
.then((response) => {
return response.json();
})
.catch((error) => console(error));
}
render(){
return (
<ul>
{this.renderDocuments()}
</ul>
);
}export default Documents;
And here is my Doc.js
const Doc = ({ doc }) => (
<li>
<p>{doc.id} - {doc.documentCode} - {doc.documentName} - {doc.issuedDate}</p>
<a onClick={this.deleteHandle(doc.id)}>Delete</a>
</li>);export default Doc;
The API and Get works fine, but when I clicked on DELETE button, it says:
_this.deleteHandle is not a function.
Please help.