export, import
export와 import는 JavaScript 모듈 시스템을 사용하여 파일 간에 코드를 구조화하고 공유하는 데 사용
export
- 특정 파일에서 함수, 변수, 클래스 등을 내보낼 수 있다.
✍ 예시 코드
import React from 'react';
class MyComponent extends React.Component {
}
export default MyComponent;
import
- 다른 파일에서 내보낸 함수, 변수, 클래스 등을 가져올 수 있다.
✍ 예시 코드
import React from 'react';
import MyComponent from './MyComponent';
class AnotherComponent extends React.Component {
render() {
return (
<div>
<p>This is another component.</p>
{}
<MyComponent />
</div>
);
}
}
export default AnotherComponent;
명명된 export 사용
- 한 파일에서 여러 항목을 내보내고 다른 파일에서 특정 항목만 가져오려면 명명된 export를 사용할 수 있다.
✍ 예시 코드
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;
import { add, subtract } from './utils';
console.log(add(5, 3));
console.log(subtract(5, 3));