首页 文章

使用axios删除请求 - React

提问于
浏览
1

我正在尝试基于Json服务器包创建小应用程序,这将帮助我记住我有空闲时间想看的电影,想要学习React和Axios,所以我用这些技术做到了,想法就在我的时候点击添加电影按钮 - 电影将被添加到Json数据库,当点击删除 - 特定电影将被删除,当点击列表 - 我将能够编辑文本,

如果我执行http://localhost:3000/movies/1之类的操作,删除有效,以显示它应删除的ID,但是有什么方法可以设置它吗?要删除连接到按钮的列表,我点击了?像http://localhost:3000/movies/ "id"?我会感激任何帮助,因为我完全不知道如何继续前进

import React from 'react';
import ReactDom from 'react-dom';
import axios from 'axios';
import List from "./list.jsx";

class Form extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            name:'',
            type:'',
            description:'',
            id:'',
            movies: [],

        }
    }

    handleChangeOne = e => {
        this.setState({
            name:e.target.value
        })
    }
    handleChangeTwo = e => {
        this.setState({
            type:e.target.value
        })
    }
    handleChangeThree = e => {
        this.setState({
            description:e.target.value
        })
    }

    handleSubmit = e => {
        e.preventDefault()
        const url = `http://localhost:3000/movies/`;
        axios.post(url, {
            name: this.state.name,
            type: this.state.type,
            description:this.state.description,
            id:this.state.id
        })
            .then(res => {
                // console.log(res);
                // console.log(res.data);
                this.setState({
                    movies:[this.state.name,this.state.type,this.state.description, this.state.id]
                })
            })
    }

    handleRemove = (e) => {
        const id = this.state.id;
        const url = `http://localhost:3000/movies/`;
        // const id = document.querySelectorAll("li").props['data-id'];
        e.preventDefault();
        axios.delete(url + id)
            .then(res => {
                console.log(res.data);
            })
            .catch((err) => {
                console.log(err);
            })
    }

    // editMovie = e => {
    //     const url = `http://localhost:3000/movies/`;
    //     e.preventDefault();
    //     const id = e.target.data("id");
    //     axios.put(url + id, {
    //             name: this.state.name,
    //             type: this.state.type,
    //             description:this.state.description,
    //     })
    //         .then(res => {
    //             console.log(res.data);
    //         })
    //         .catch((err) => {
    //             console.log(err);
    //         })
    // }


    render() {
        return (

            <form onSubmit={this.handleSubmit}>
                <input type="text" placeholder="movie" onChange={this.handleChangeOne}/>
                <input type="text" placeholder="type of movie" onChange={this.handleChangeTwo}/>
                <textarea cols={40} rows={5} placeholder="description of the movie" onChange={this.handleChangeThree}></textarea>
                <input type="submit" value="Add movie"></input>
                <List removeClick={this.handleRemove} editClick={this.editMovie}/>
            </form>
        )
    }
}

export default Form

列表:

import React from 'react';
import ReactDom from 'react-dom';
import axios from 'axios';


class List extends React.Component{
    constructor(props){
        super(props)
        this.state = {
            movies: [],

        }
    }

    componentDidMount() {
        const url = `http://localhost:3000/movies`;
        console.log(url);
        axios.get(url)
            .then(res => {
                console.log(res.data);
                const movies = res.data;
                this.setState({
                    movies: movies
                })
            })
            .catch((err) => {
                console.log(err);
            })

    }

    // editMovie =(e) => {
    //     console.log("it works with edit!");
    //     if (typeof this.props.editClick === "function") {
    //         this.props.editClick(e)
    //     } else {
    //         console.log("Doesn't work with edit");
    //     }
    // }

    removeMovie =(e) => {
        console.log("it works with remove!");
        if (typeof this.props.removeClick === "function") {
            this.props.removeClick(e)
        } else {
            console.log("Doesn't work with remove");
        }
    }


    render(){
        let movies = this.state.movies.map(e =>
            <ul onClick={this.editMovie}>
                <li data-id={e.id}>
                    {e.name}
                </li>
                <li data-id={e.id}>
                    {e.type}
                </li>
                <li data-id={e.id}>
                    {e.description}
                </li>
                <button type="submit" onClick={this.removeMovie}>Delete</button>
            </ul>)

        return(
            <div>
                {movies}
            </div>
        )
    }
}

export default List;

杰森的一部分

{
  "movies": [
    {
      "id": 1,
      "name": "Kongi",
      "type": "drama",
      "description": "movie about monkey"
    },
    {
      "id": 2,
      "name": "Silent Hill",
      "type": "thriller",
      "description": "movie about monsters"
    },
    {
      "name": "Harry potter",
      "type": "fantasy",
      "description": "movie about magic and glory",
      "id": 3
    }
  ]
}

1 回答

  • 0

    您可以将 movie 对象传递给 List 组件中的 removeMovie 函数,并将其传递给 this.props.removeClick 函数 . 然后,您可以将 movieid 用于您的请求,如果DELETE请求成功,则从状态中删除 movie .

    Example

    class Form extends React.Component {
      handleRemove = movie => {
        const url = `http://localhost:3000/movies/${movie.id}`;
    
        axios
          .delete(url)
          .then(res => {
            this.setState(previousState => {
              return {
                movies: previousState.movies.filter(m => m.id !== movie.id)
              };
            });
          })
          .catch(err => {
            console.log(err);
          });
      };
    
      // ...
    }
    
    class List extends React.Component {
      removeMovie = (e, movie) => {
        e.preventDefault();
    
        if (this.props.removeClick) {
          this.props.removeClick(movie);
        }
      };
    
      // ...
    
      render() {
        return (
          <div>
            {this.state.movies.map(movie => (
              <ul onClick={this.editMovie}>
                <li data-id={movie.id}>{movie.name}</li>
                <li data-id={movie.id}>{movie.type}</li>
                <li data-id={movie.id}>{movie.description}</li>
                <button type="submit" onClick={e => this.removeMovie(e, movie)}>
                  Delete
                </button>
              </ul>
            ))}
          </div>
        );
      }
    }
    

相关问题