I'm working on a movie rental project called MoVid, which is in React and JavaScript. Here's my code:
App.js
import React, { Component } from "react";
import Movies from "./components/movies";
import "./App.css";
function App() {
return (
<main className="container">
<Movies></Movies>
</main>
);
}
export default App;
Movie.jsx
import React, { Component } from "react";
import { getMovies } from "../services/fakeMovieService";
class Movies extends Component {
state = {};
render() {
return (
<table className="table">
<thead>
<tr>
<th>Title</th>
<th>Genre</th>
<th>Stock</th>
<th>Rate</th>
</tr>
</thead>
<tbody>
{this.state.movies.map(movie => (
<tr>
<td>{movie.title}</td>
<td>{movie.genre.name}</td>
<td>{movie.numberInStock}</td>
<td>{movie.dailyRentalRate}</td>
</tr>
))}
</tbody>
</table>
);
}
}
export default Movies;
fakeGenreService.js
export const genres = [
{ _id: "5b21ca3eeb7f6fbccd471818", name: "Action" },
{ _id: "5b21ca3eeb7f6fbccd471814", name: "Comedy" },
{ _id: "5b21ca3eeb7f6fbccd471820", name: "Thriller" }
];
export function getGenres() {
return genres.filter(g => g);
}
fakeMovieService.js
import * as genresAPI from "./fakeGenreService";
const movies = [
{
_id: "5b21ca3eeb7f6fbccd471815",
title: "Terminator",
genre: { _id: "5b21ca3eeb7f6fbccd471818", name: "Action" },
numberInStock: 6,
dailyRentalRate: 2.5,
publishDate: "2018-01-03T19:04:28.809Z"
},
{
_id: "5b21ca3eeb7f6fbccd471816",
title: "Die Hard",
genre: { _id: "5b21ca3eeb7f6fbccd471818", name: "Action" },
numberInStock: 5,
dailyRentalRate: 2.5
},
{
_id: "5b21ca3eeb7f6fbccd471817",
title: "Get Out",
genre: { _id: "5b21ca3eeb7f6fbccd471820", name: "Thriller" },
numberInStock: 8,
dailyRentalRate: 3.5
}];
export function getMovies() {
return movies;
}
export function getMovie(id) {
return movies.find(m => m._id === id);
}
export function saveMovie(movie) {
let movieInDb = movies.find(m => m._id === movie._id) || {};
movieInDb.name = movie.name;
movieInDb.genre = genresAPI.genres.find(g => g._id === movie.genreId);
movieInDb.numberInStock = movie.numberInStock;
movieInDb.dailyRentalRate = movie.dailyRentalRate;
if (!movieInDb._id) {
movieInDb._id = Date.now();
movies.push(movieInDb);
}
return movieInDb;
}
export function deleteMovie(id) {
let movieInDb = movies.find(m => m._id === id);
movies.splice(movies.indexOf(movieInDb), 1);
return movieInDb;
}
I have looked at:
React code throwing “TypeError: this.props.data.map is not a function”
React JS - Uncaught TypeError: this.props.data.map is not a function
But they don't help.
The other 3 files of code is in here. If you need more information or code, please ask me! Thanks in advance!