Exporting and importing lets you share parts of your React app between files. It helps keep code organized and reusable.
Exporting and importing components in React
export default ComponentName; // or export { ComponentName }; // Importing import ComponentName from './ComponentFile'; // or import { ComponentName } from './ComponentFile';
Default export means you export one main thing from a file.
Named export lets you export multiple things from one file.
function Button() { return <button>Click me</button>; } export default Button;
export function Header() { return <h1>Welcome</h1>; }
import Button from './Button'; function App() { return <Button />; }
import { Header } from './Header'; function App() { return <Header />; }
This example shows a Button component exported as default from Button.js. Then App.js imports and uses it inside a div with a heading.
/* Button.js */ import React from 'react'; export default function Button() { return <button>Click me</button>; } /* App.js */ import React from 'react'; import Button from './Button'; export default function App() { return ( <div> <h2>My App</h2> <Button /> </div> ); }
Always use the correct import style matching how the component was exported.
File paths in imports are relative and usually start with './' for the same folder.
Using default exports is simpler when you export one main component per file.
Exporting lets you share components between files.
Use default export for one main component, named export for multiple.
Import components with matching syntax to use them in your app.
Practice
Solution
Step 1: Understand the purpose of export
Exporting a component allows it to be shared and used in other files.Step 2: Connect export with import usage
Without export, other files cannot import and use the component.Final Answer:
To use the component in other files by importing it -> Option CQuick Check:
Export shares components [OK]
- Thinking export improves speed
- Believing export hides components
- Assuming export adds styles automatically
Solution
Step 1: Recall default export syntax
The correct syntax is 'export default' followed by the function declaration.Step 2: Check each option's syntax
export default function MyComponent() { returnHello; } matches the correct syntax; others have invalid order or keywords.Final Answer:
export default function MyComponent() { return <div>Hello</div>; } -> Option BQuick Check:
Default export uses 'export default' [OK]
- Placing 'default' after function name
- Using '=' with export incorrectly
- Mixing 'export' and 'default' order
App is rendered?// Button.js
export function Button() { return <button>Click</button>; }// App.js
import { Button } from './Button';
export default function App() { return <div><Button /></div>; }Solution
Step 1: Understand named export and import
Button is exported as a named export and imported with curly braces correctly.Step 2: Analyze rendering in App component
App renders a div containing the Button component, which returns a button element with text 'Click'.Final Answer:
<div><button>Click</button></div> -> Option AQuick Check:
Named export/import works = output includes button inside div [OK]
- Importing named export without braces
- Expecting Button text instead of button element
- Confusing default and named exports
import Button from './Button';Given that
Button.js contains:export function Button() { return <button>Click</button>; }Solution
Step 1: Check export type in Button.js
Button is exported as a named export using 'export function Button'.Step 2: Compare with import syntax
Importing without braces means default import, but Button is not default exported.Final Answer:
Button is a named export but imported as default -> Option AQuick Check:
Named export must import with braces [OK]
- Importing named export without braces
- Assuming default export without checking
- Ignoring export type in source file
Widgets.js:export function WidgetA() { return <div>A</div>; }
export function WidgetB() { return <div>B</div>; }How should you import both components in
Dashboard.js to use them?Solution
Step 1: Identify export type in Widgets.js
Both WidgetA and WidgetB are named exports.Step 2: Choose correct import syntax for multiple named exports
Use curly braces with both names inside: import { WidgetA, WidgetB } from './Widgets';Final Answer:
import { WidgetA, WidgetB } from './Widgets'; -> Option DQuick Check:
Multiple named exports import with braces and commas [OK]
- Trying to import multiple named exports as default
- Using separate default imports for each named export
- Using import * without accessing properties
Ready to prove it?
Pass the test and this pattern moves unknown
