javascript / beginner
Snippet
ES Modules: Export and Import
React apps use ES Modules to organize code. 'export default' allows a component to be shared, while 'import' brings it into another file for use.
snippet.js
1
2
3
4
5
6
7
// In MyComponent.jsexport default function MyComponent() {return <div>Modular Code</div>;}// In App.jsimport MyComponent from './MyComponent';
react
Breakdown
1
export default function MyComponent() {
Makes this function the primary export of the file.
2
import MyComponent from './MyComponent';
Loads the component from its specific file path.