Hi there, I’m stuck with an exercise. I’d like to create a Like counter but my handleLikeCount() doesn’t work.
Here is my movies component containing the handleLikeCount() :
import React, { Component } from ‘react’;
import { getMovies } from ‘…/services/fakeMovieService’;
class Movies extends Component {
state = {
movies: getMovies(),
};
handleDelete = movie => {
const movies = this.state.movies.filter(m => m._id !== movie._id);
this.setState({ movies })
};
handleLikeCount = (movie) => {
const movies = [... this.state.movies];
const index = movies.indexOf(movie);
movies[index] = {...movie};
movies[index].movieJaime++;
this.setState({ movies })
console.log(this.state.movies);
};
render() {
const { length : count} = this.state.movies;
if(count === 0) return <p>There is no movie in the database</p>;
return (
<React.Fragment>
<p>There are { count } movies in the database</p>
<table className="table">
<thead>
<tr>
<th>Name</th>
<th>Genre</th>
<th>Stock</th>
<th>Rate</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{this.state.movies.map(movie => (
<tr key={movie._id}>
<td>{movie.title}</td>
<td>{movie.genre.name}</td>
<td>{movie.numberInStock}</td>
<td>{movie.dailyRentalRate}</td>
<td>
<button
className="btn btn-danger btn-sm"
onClick={() => this.handleDelete(movie)}>Delete</button>
</td>
<td>
<button
className="btn btn-primary"
onClick={() => this.handleLikeCount(movie)}>
Like
<span className="badge badge-light">
{movie.LikeCount}
</span>
</button>
</td>
</tr>
))}
</tbody>
</table>
</React.Fragment>
);
}
}
export default Movies;
Thanks for any help