프로젝트, 개발자마다 참조하는 방식이 달라서 이해하지 않고 복붙하기 바빴다.
많은 index.js 파일을 만들고 써왔지만, 정작 이해하지않고 썼다는게 어느 순간 머리가 띵한 느낌이 들었다.
오늘 정리하면서 검색하지 않고 참조하는 습관을 길러야겠다!
// @components/index.js
export const EXPORT_VARIABLE = {}
// App.js
import { EXPORT_VARIABLE } from '@components'
// @components/index.js
import IMPORT_COMP1 from '/import_comp_path'
import IMPORT_COMP2 from '/import_comp_path'
const Comp = { IMPORT_COMP1, IMPORT_COMP2}
export default Comp
// App.js
import Comp from '@components'
Comp.IMPORT_COMP1
// @components/index.js
export { IMPORT_COMP1 as COMP1 } from './import_comp_path'
export { IMPORT_COMP2 as COMP2 } from './import_comp_path'
// App.js
import { COMP1, COMP2 } from '@components'
// @components/index.js
export { IMPORT_COMP1 as default, IMPORT_COMP2 };
// App.js
import IMPORT_COMP, { IMPORT_COMP2 } from '@components'
# exportExample.js
// 방법 1
function exam1 = () => { console.log('exam1 func') }
// 방법2
export function exam2 = () => { console.log('exam2') }
export { exam1 };
# imortExample.js
import { exam1, exam2 } from '/exportExample'
import * as exam from '/exportExample'
exam.exam1();
그래도 각 프로젝트마다 달라서 헷갈렸는데 이제야 정리가 되는 기분이다.
사이드 프로젝트에서는
Page: export default(Default exports)
Module: export(Named exports)
이렇게 사용해서 써야겠다!