react에서 여러 컴포넌트를 index에 모아서 한 줄로 import해오기!

이온·2023년 3월 14일
1

두번째 프로젝트

목록 보기
3/6

기존 코드

각각의 컴포넌트 파일에서 컴포넌트를 export하면 그 컴포넌트들이 필요한 페이지에서 각각을 import해온다.

ShelterPage.js

import { ShelterMap } from "../../components/shelter/ShelterMap";
import { ShelterList } from "../../components/shelter/ShelterList";
import { ShelterTable } from "../../components/shelter/ShelterTable";

바꾼 코드


컴포넌트들이 있는 폴더 안에 index.js라는 파일을 생성해서 여기서 export를 한번에 해준다. 그러면 page에서 컴포넌트들을 import해올때 한줄로 간결하게 작성될 수 있다.

index.js

export * from "./ShelterList";
export * from "./ShelterMap";
export * from "./ShelterRoadview";

ShelterPage.js

import { ShelterMap, ShelterList, ShelterTable } from "../../components/shelter";
profile
👩🏻‍💻

0개의 댓글