ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • #6 Presenters
    React 2019. 11. 19. 21:39

    Presenter 구조

    • HomePresenter.js
    import React from "react";
    import ProTypes from "prop-types";
    import styled from "styled-components";
    
    const HomePresenter = (nowPlaying, upComing, popular, error, loading) => null;
    
    HomePresenter.ProTypes = {
      nowPlaying: ProTypes.array,
      upComing: ProTypes.array,
      popular: ProTypes.array,
      error: ProTypes.string,
      loading: ProTypes.bool.isRequired
    };
    
    export default HomePresenter;
    
    • HomeContainer.js
    import React from "react";
    import HomePresenter from "./HomePresenter";
    import { moviesApi } from "../../api";
    
    export default class extends React.Component {
      state = {
        nowPlaying: null,
        upcoming: null,
        popular: null,
        error: null,
        loading: true
      };
    
      async componentDidMount() {
        try {
          const {
            data: { results: nowPlaying }
          } = await moviesApi.nowPlaying();
          const {
            data: { results: upcoming }
          } = await moviesApi.upcoming();
          const {
            data: { results: popular }
          } = await moviesApi.popular;
          this.setState({
            nowPlaying,
            upcoming,
            popular
          });
        } catch (error) {
          this.setState({
            error: "Can't find movie information."
          });
        } finally {
          this.setState({
            loading: false
          });
        }
      }
    
      render() {
        const { nowPlaying, upcoming, popular, error, loading } = this.state;
        return (
          <HomePresenter
            nowPlaying={nowPlaying}
            upcoming={upcoming}
            popular={popular}
            error={error}
            loading={loading}
          />
        );
      }
    }
    

     

    • TV, Detail,SearchPresenter.js 파일에도 위와같은 구조로 작성해준다.

    'React' 카테고리의 다른 글

    Poster Component  (0) 2019.11.21
    Children이란?  (0) 2019.11.19
    let으로 할당 제거(Destructuring assignment)  (0) 2019.11.19
    Detail Container 2  (0) 2019.11.18
    Detail Container 1  (0) 2019.11.14

    댓글

Designed by Tistory.