Answer the question
In order to leave comments, you need to log in
How to remove an error when compiling a react application?
as part of the training, I write my carcass. Now I am writing a function for deleting a task by clicking on a button. here is the react-element code:
import React from 'react'
import axios from "axios"
class LoadTask extends React.Component{
constructor(props) {
super(props);
this.state = {items:[],
};
}
async componentDidMount() {
try {axios.get('http://localhost:5000/api/getTask')
.then(response => this.setState({items: Object.values(response.data)}));}
catch ( error ) {
console.log(error)
};
}
render() {
async function todelete(todel) {
try{axios.delete("http://localhost:5000/toDel"), {data:{todel}}}
catch(error){
console.log(error);
if(error){
return(console.log("error"));
}else{
return(alert("tesk deleted!"))
}
}
}
return(
<div className="loadTaskWrap">
<p>Здесь ты можешь просмотреть существующие таски:</p>
<div className="desP">
<div>{this.state.items.map((item) =>
(<div key={item.description} className='styleTask' >
<p className="stDesk" >{item.description}:</p>
<div>{item.task}
</div>
<div>{item.addDate}</div>
<button onClick={()=>todelete(item._id)}>Удалить задачу</button>
<button>Отметить как выполненную</button>
</div>))}</div>
</div>
</div>
)
}
}
export default LoadTask
Answer the question
In order to leave comments, you need to log in
Solved by moving the function out of render and calling it via this. todelete. You probably need to re-read the documentation for react
return(console.log("error"));
return(alert("tesk deleted!"))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question