src 하위 폴더의 파일에서 상대 경로로 다른 폴더의 파일에 접근할 때 폴더 구조가 복잡하고 깊은 경우../../components/A 형태로 ../이 많이 사용되고 가독성이 떨어진다.
import A from '../../components/A';
import CommonB from '../../components/common/B';
import commonStyle from '../../lib/styles/common';절대 경로를 사용하면 특정 폴더를 기준으로 import path를 설정할 수 있다.
- 최상위 폴더 아래에
jsconfig.json(typescript인 경우tsconfig.json) 파일을 생성한다 baseUrl설정을 추가한다. 아래의 경우src폴더를 기준으로 절대 경로를 사용할 수 있다.
// jsconfig.json
{
"compilerOptions": {
"baseUrl": "src"
},
"include": ["src"]
}// 이전
import A from '../../components/A';
// baseUrl 추가 후
import A from 'components/A';출처
'React' 카테고리의 다른 글
| [Redux] Redux 기초 (0) | 2020.12.23 |
|---|---|
| [React] Error: Node Sass version 5.0.0 is incompatible with ^4.0.0 (0) | 2020.12.22 |
| [ React ] React.PureComponent (0) | 2018.01.06 |
댓글