React

Detail Container 2

정중식 2019. 11. 18. 23:16

2. url경로에 어떤 숫자가 있는지..? (/movie/1 <=  & /show/1 <= 여기에 어떤 숫자가 있는지 알아야함)

  • DetailContainer.js

import React from "react";
import DetailPresenter from "./DetailPresenter";
import { moviesApi, tvApi } from "../../api";

export default class extends React.Component {
  constructor(props) {
    super(props);
    const {
      location: { pathname }
    } = props;
    this.state = {
      result: null,
      error: null,
      loading: true,
      isMovie: pathname.includes("/movie/")
    };
  } // 클래스가 생성되어진것

  async componentDidMount() {
    const {
      match: {
        params: { id }
      },
      history: { push }
    } = this.props;
    const { isMovie } = this.state;
    const parsedId = parseInt(id);
    if (isNaN(parsedId)) {
      return push("/");
    }
    let result = null;
    try {
      if (isMovie) {
        const request = await moviesApi.movieDetail(parsedId);
        result = request.data;
      } else {
        const request = await tvApi.showDetail(parsedId);
        result = request.data;
      }
      console.log(result);
    } catch (error) {
      this.setState({ error: "Can't find anything." });
    } finally {
      this.setState({ loading: false, result });
    }
  }

  render() {
    // console.log(this.props); => match.params 정보를 얻을수잇다.
    const { result, error, loading } = this.state;
    console.log(this.state);
    return <DetailPresenter result={result} error={error} loading={loading} />;
  }
}
 

[번역] React ES6 — Class constructor에서의 super()

본 글은 http://cheng.logdown.com/posts/2016/03/26/683329를 한국어로 번역한 글입니다.

medium.com