출처: https://bumcrush.tistory.com/182 [맑음때때로 여름]

상세 컨텐츠

본문 제목

lazy component, Switch

React

by 장동균 2020. 7. 30. 15:38

본문

https://ko.reactjs.org/docs/code-splitting.html

 

코드 분할 – React

A JavaScript library for building user interfaces

ko.reactjs.org

import React, { Suspense } from 'react';

const OtherComponent = React.lazy(() => import('./OtherComponent'));

function MyComponent() {
  return (
    <div>
      <Suspense fallback={<div>Loading...</div>}>
        <OtherComponent />
      </Suspense>
    </div>
  );
}

suspense로 lazy component를 감싸게 되면 suspense 부분이 실행되다가 lazy component 부분의 rendering이 완료되면 suspense에서 lazy component로 넘어간다.

 

const App=()=> {
  return (
    <Suspense fallback={(<div>Loading...</div>)}>
      <NavBar />
      <div style={{ paddingTop: '75px', minHeight: 'calc(100vh - 80px)' }}>
        <Switch> 
          {/* 이 아래의 페이지들 중 하나만을 선택할 거기 때문에 switch 구문으로 감싼다. */}
          <Route exact path="/" component={Auth(LandingPage, null)} />
          <Route exact path="/login" component={Auth(LoginPage, false)} />
          <Route exact path="/register" component={Auth(RegisterPage, false)} />
          <Route exact path="/video/upload" component={Auth(UploadVideoPage, true)} />
          <Route exact path="/video/:videoId" component={Auth(DetailVideoPage, null)} />
          <Route exact path="/subscription" component={Auth(SubscriptionPage, null)} />
        </Switch>
      </div>
      <Footer />
    </Suspense>
  );
}

아래의 route들 중 하나의 route로만 들어가기 때문에 switch구문으로 이들을 한번에 감싸준다.

'React' 카테고리의 다른 글

Google Map  (0) 2020.08.05
antd Carousel(auto), a href  (0) 2020.08.04
map method  (0) 2020.07.29
Link to, history.push 의 차이  (0) 2020.07.27
참고하기 좋은 사이트들을 정리하자!  (0) 2020.07.24

관련글 더보기

댓글 영역