번들 파일 분석

yarn add -D cra-bundle-analyzer
npx cra-bundle-analyzer

Untitled

Untitled


모달 코드 분리하기

  1. App.js 파일에서 코드 분할과 지연 로딩을 위해 리액트 라이브러리의 Suspense 컴포넌트와 lazy 함수를 불러옴
  2. 그런 다음 분할하고자 하는 컴포넌트인 ImageModal 컴포넌트를 import 함수와 함께 lazy 함수의 인자로 넘겨 줌
  3. ImageModal이 로드되기 전에 발생하는 에러를 방지하기 위해, Suspense 컴포넌트로 LazyImageModal 컴포넌트를 감싸줘야 함
import React, { useState, Suspense, lazy } from 'react'
//import ImageModal from './components/ImageModal'

const LazyImageModal = lazy(() => import('./components/ImageModal'));

function App() {
    const [showModal, setShowModal] = useState(false)

    return (
        <div className="App">
            <Suspense fallback={null}>
                <Header />
                <InfoTable />
                <ButtonModal onClick={() => { setShowModal(true) }}>올림픽 사진 보기</ButtonModal>
                <SurveyChart />
                <Footer />
                {showModal ? <LazyImageModal closeModal={() => { setShowModal(false) }} /> : null}
            </Suspense>
        </div>
    )
}