-
React - Create TV Container and PresenterProject using React/Cloning Netflix 2021. 5. 9. 10:51
TV/TVContainer.js
import { tvApi } from "api"; import React from "react"; import TVPresenter from "./TVPresenter"; export default class extends React.Component { state = { loading: true, airingToday: null, topRated: null, popular: null, error: null, }; async componentDidMount() { try { const { data: { results: airingToday }, } = await tvApi.airingToday(); const { data: { results: topRated }, } = await tvApi.topRated(); const { data: { results: popular }, } = await tvApi.popular(); this.setState({ airingToday, topRated, popular, }); } catch { this.setState({ error: "Can't find movie information.", }); } finally { this.setState({ loading: false, }); } } render() { const { airingToday, topRated, popular, error, loading } = this.state; return ( <TVPresenter airingToday={airingToday} topRated={topRated} popular={popular} error={error} loading={loading} /> ); } }
TV/TVPresenter.js
import Helmet from "react-helmet"; import styled from "styled-components"; import PropTypes from "prop-types"; import Loader from "Components/Loader"; import Poster from "Components/Poster"; import Section from "Components/Section"; const Container = styled.div` padding: 10px; `; const TVPresenter = ({ airingToday, topRated, popular, error, loading }) => ( <> <Helmet> <title>TV | Nomflix</title> </Helmet> {loading ? ( <Loader /> ) : ( <Container> <Section title="Now Playing"> {airingToday.map((tv) => ( <Poster key={tv.id} id={tv.id} imageUrl={tv.poster_path} title={tv.original_name} rating={tv.vote_average} year={tv.first_air_date.substring(0, 4)} /> ))} </Section> <Section title="Upcoming"> {topRated.map((tv) => ( <Poster key={tv.id} id={tv.id} imageUrl={tv.poster_path} title={tv.original_name} rating={tv.vote_average} year={tv.first_air_date.substring(0, 4)} /> ))} </Section> <Section title="Popular"> {popular.map((tv) => ( <Poster key={tv.id} id={tv.id} imageUrl={tv.poster_path} title={tv.original_name} rating={tv.vote_average} year={tv.first_air_date.substring(0, 4)} /> ))} </Section> </Container> )} </> ); TVPresenter.propTypes = { topRated: PropTypes.array, popular: PropTypes.array, airingToday: PropTypes.array, loading: PropTypes.bool.isRequired, error: PropTypes.string, }; export default TVPresenter;
key
nowPlaying, upcoming, popular는 모두 배열이다. 이들을 map을 이용해서 <Poster>를 부르고 있다. 이 때 리스트 안에 있는 <Poster>에 key 값을 추가해야만 한다.
prop-types
설치
$ npm i prop-types
개요
prop-types는 prop의 type을 정의한다. prop을 받을 component 뒤에 method 형식으로 propTypes를 하고 객체를 정의한다.
위 코드와 같이 type을 정의하면 MoviePresenter가 받아들이는 prop이 옳지 않은 type일 때 error를 일으켜 개발자가 실수 하는 것을 막아준다. 만약 React를 typescript로 짰다면 prop-types는 필요가 없다.
참고 자료
- 노마드 코더의 React 멤버쉽 강의
- prop-types
- React propTypes 설명
- Children prop
소스 코드
github.com/zpskek/Nomflix-v2/commit/f2da2df218f01be0c3210efe955526b4fb0175bd
'Project using React > Cloning Netflix' 카테고리의 다른 글
React - Create Detail Container and Presenter (0) 2021.05.09 React - Search Container and Presenter (0) 2021.05.09 React - Create MoviePresenter (0) 2021.05.09 React - Create Loader.js (0) 2021.05.09 React - Create MovieContainer (0) 2021.05.09