K
K
Kentavr162021-06-09 20:14:53
React
Kentavr16, 2021-06-09 20:14:53

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


the todelete function should delete the selected task. But when I try to run the react app, I get an error in the console: Line 26:16: Expected an assignment or function call and instead saw an expression no-unused-expressions.

I am doubly puzzled, since it is worth writing something like alert (todel) in todelete, how everything works. What's my mistake?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kentavr16, 2021-06-09
@Kentavr16

Solved by moving the function out of render and calling it via this. todelete. You probably need to re-read the documentation for react

D
Dima Pautov, 2021-06-09
@bootd

return(console.log("error"));
return(alert("tesk deleted!"))

Remove return from catch. Either return an error or return nothing

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question