ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • let으로 할당 제거(Destructuring assignment)
    React 2019. 11. 19. 00:02
    • Destructuring assignment이란 es6에 추가된 새로운 표현식이다.
    • ({ data: result } = await moviesApi.movieDetail(parsedId))
      • ()한 이유(할당 제거): 니코왈:"const {data : result} = await .....를 수행하면 그런 다음 try {} 안에 'result'const를 생성했기 때문에 finally {}에서 'result'변수에 액세스하여 상태를 설정할 수 없습니다. {}"
      • 내가 이해한 내용: finally{} 에서 result 변수에 접근 하기위해서 할당 제거를 사용했다

     

    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) {
            ({ data: result } = await moviesApi.movieDetail(parsedId));
          } else {
            ({ data: result } = await tvApi.showDetail(parsedId));
          }
        } 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(result);
        return <DetailPresenter result={result} error={error} loading={loading} />;
      }
    }
    

    'React' 카테고리의 다른 글

    Children이란?  (0) 2019.11.19
    #6 Presenters  (0) 2019.11.19
    Detail Container 2  (0) 2019.11.18
    Detail Container 1  (0) 2019.11.14
    Home ,TV, Search, Detail Container  (0) 2019.11.14

    댓글

Designed by Tistory.