1

I am trying to display data from another file into a Table component in React. The data is getting displayed but the delete method isn't working. I am not sure what mistake I have done over here. I am not getting any console errors and the delete button does say it's clicked in the console.

Movie.js

import React, { Component } from 'react'
import { Table } from 'react-bootstrap';
import Data from './data';

export default class Movie extends Component {
    constructor(){
        super();
        this.state={
            movies:Data
        }
        console.log(this.state.movies)
        this.handleDelete=this.handleDelete.bind(this);
    }

    handleDelete= movie=>{
        const del=this.state.movies.filter(item => item !== movie.id);
        this.setState({
            movie:del
        })
        console.log("clicked")
    }
    render() {
        //const store=this.state.movies.id;
        const show= this.state.movies.map((movie,i)=>{
            return(
       <tr key={i}>
         <td>{movie.id}</td>
         <td>{movie.name}</td>
         <td>{movie.price}</td>
         <td>{movie.rating}</td>
        <td> <button onClick={()=>this.handleDelete(movie)}>Delete</button></td>
       </tr>
        )})
        return (
            <div>
                <Table striped bordered hover>
                <thead>
                    <tr>
                    <th>#</th>
                    <th>Movie</th>
                    <th>Price</th>
                    <th>Rating</th>
                    </tr>
                </thead>
                <tbody>
                    {show}
                </tbody>

</Table>
            </div>
        )
    }
}

Data.js

  const i=[
    {
    id:1,
    name:"Captain america",
    price:"$10",
    rating:'5 stars'
    },
    {
        id:2,
        name:"Batman",
        price:"$12",
        rating:'4 stars'
    },
    {
        id:3,
        name:"Arrow",
        price:"$8",
        rating:'3 stars'
    }
]
const data=i;

export default data;
1
  • Don't use index as the key Lists and Keys Commented Feb 20, 2020 at 22:58

2 Answers 2

2

First you have a typo and second you're not checking if the movie's id is not equal to current loops item id.

// item.id !== movie.id
const del = this.state.movies.filter(item => item.id !== movie.id);

// movies not movie
this.setState({
   movies: del
})

Also don't ever use index as key in production (unless the items are static), it's an anti-pattern. Instead, use the unique id <tr key={movie.id}>

Sign up to request clarification or add additional context in comments.

Comments

0

in your handleDelete function

handleDelete= movie=>{
        const del=this.state.movies.filter(item => item !== movie.id);
        this.setState({
            movie:del <=== movies not movie!
        })
        console.log("clicked")
    }

i think you just typo'd movie instead of movies when setting state

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.