728x90
Styling the Movies
App.js와 Movies.js를 좀 html스럽게 수정해보자.
App.js
import React from "react";
import axios from "axios";
import Movie from "./Movies";
class App extends React.Component {
state = {
isLoading: true,
movies: [],
};
getMovies = async () => {
const {
data: {
data: { movies },
},
} = await axios.get("https://yts-proxy.now.sh/list_movies.json");
this.setState({ isLoading: false, movies });
};
componentDidMount() {
this.getMovies();
}
render() {
const { isLoading, movies } = this.state;
return (
<section class="container">
{isLoading ? (
<div class="loader">
<span class="loader__text">Loading...</span>
</div>
) : (
<div class="movies">
{movies.map((movie) => (
<Movie
key={movie.id}
id={movie.id}
year={movie.year}
title={movie.title}
summary={movie.summary}
poster={movie.medium_cover_image}
/>
))}
</div>
)}
</section>
);
}
}
export default App;
Movies.js
// render movies
import React from "react";
import PropTypes from "prop-types";
function Movie({ id, year, title, summary, poster }) {
return (
<div class="movie">
<img src={poster} alt={title} title={title} />
<div class="movie__data">
<h3 class="movie__title">{title}</h3>
<h5 class="movie__year">{year}</h5>
<p class="movie__summary">{summary}</p>
</div>
</div>
);
}
Movie.propTyes = {
id: PropTypes.number.isRequired,
year: PropTypes.number.isRequired,
title: PropTypes.string.isRequired,
summary: PropTypes.string.isRequired,
poster: PropTypes.string.isRequired,
};
export default Movie;
css를 적용하기 위해서 css파일을 만들어 작성하고, 적용시킬 JS파일에 import하면 된다.
import "./Movies.css";
여기서 오류가 하나 일어나는데, html코드를 작성할때 class 요소를 className으로 써주어야 react가 js의 class와 혼동하지 않는다.
장르도 추가해보자.
App.js
import React from "react";
import axios from "axios";
import Movie from "./Movies";
import "./App.css";
class App extends React.Component {
state = {
isLoading: true,
movies: [],
};
getMovies = async () => {
const {
data: {
data: { movies },
},
} = await axios.get("https://yts-proxy.now.sh/list_movies.json");
this.setState({ isLoading: false, movies });
};
componentDidMount() {
this.getMovies();
}
render() {
const { isLoading, movies } = this.state;
return (
<section className="container">
{isLoading ? (
<div className="loader">
<span className="loader__text">Loading...</span>
</div>
) : (
<div className="movies">
{movies.map((movie) => (
<Movie
key={movie.id}
id={movie.id}
year={movie.year}
title={movie.title}
summary={movie.summary}
poster={movie.medium_cover_image}
genres={movie.genres}
/>
))}
</div>
)}
</section>
);
}
}
export default App;
Movies.js
// render movies
import React from "react";
import PropTypes from "prop-types";
import "./Movies.css";
function Movie({ year, title, summary, poster, genres }) {
return (
<div className="movie">
<img src={poster} alt={title} title={title} />
<div className="movie__data">
<h3 className="movie__title">{title}</h3>
<h5 className="movie__year">{year}</h5>
<p className="movie__summary">{summary}</p>
<ul className="genres">
{genres.map((genre, index) => (
<li key={index} className="genres__genre">
{genre}
</li>
))}
</ul>
</div>
</div>
);
}
Movie.propTyes = {
id: PropTypes.number.isRequired,
year: PropTypes.number.isRequired,
title: PropTypes.string.isRequired,
summary: PropTypes.string.isRequired,
poster: PropTypes.string.isRequired,
genres: PropTypes.arrayOf(PropTypes.string).isRequired,
};
export default Movie;
CSS 적용 후
728x90
300x250