首页 > 解决方案 > 我的删除功能将无法使用反应

问题描述

删除按钮不使用反应

我可以在邮递员中执行 axios 调用来删除,但是当我尝试在前端进行响应时,我的删除功能将不起作用。

<button onSubmit = {this.handleSubmit}> Delete </button>

// below is the function I'm trying to use to delete an item from my database

async handleSubmit(event)  { 
        event.preventDefault()
        await axios.delete(`http://localhost:3000/brands/${this.props.match.params.brand_id}/guitars/${this.props.match.params.id}`)
      console.log("pressed")
    }

// the backend is built in ruby and in postman the delete route works

我不明白为什么按钮不起作用。

我希望在刷新页面时收到错误,因为特定项目(吉他)和 url 不再在数据库中,或者,我的重定向路由将起作用,我将被重定向到显示所有项目(吉他)的页面特定品牌。

标签: javascriptnode.jsreactjsecmascript-6

解决方案


改为 onClick 不是 onSubmit。按钮没有 onSubmit 事件

 async handleSubmit(event)  { 
      event.preventDefault()
      await axios.delete()
      console.log("pressed")
  }


  render() {
    return (
      <div>
        <Hello name={this.state.name} />
        <p>
          Start editing to see some magic happen :)
        </p>
         <button onClick={(e) => this.handleSubmit(e)}> Delete </button>
      </div>
    );
  }

代码在这里


推荐阅读