Bird
Raised Fist0
Reactframework~5 mins

Exporting and importing components in React - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the purpose of exporting a component in React?
Exporting a component allows it to be used in other files by making it available outside its own file.
Click to reveal answer
beginner
What is the difference between a default export and a named export?
A default export lets you export one main thing from a file and import it without curly braces. Named exports allow exporting multiple things and require curly braces when importing.
Click to reveal answer
beginner
How do you import a default exported component named <code>Header</code> from a file <code>Header.js</code>?
Use <code>import Header from './Header';</code> to import the default exported component.
Click to reveal answer
beginner
How do you import a named exported component called <code>Footer</code> from <code>Footer.js</code>?
Use <code>import { Footer } from './Footer';</code> with curly braces to import a named export.
Click to reveal answer
intermediate
Why might you choose named exports over default exports in React components?
Named exports help keep imports clear and allow exporting multiple components from one file, making code easier to manage and avoid naming conflicts.
Click to reveal answer
Which syntax correctly exports a React component as the default export?
Aexport { MyComponent };
Bexport function MyComponent() {}
Cimport MyComponent from './MyComponent';
Dexport default function MyComponent() {}
How do you import a named export called Button from a file?
Aimport Button from './Button';
Bimport { Button } from './Button';
Cimport * as Button from './Button';
Dexport { Button } from './Button';
What happens if you try to import a default export using curly braces?
AIt imports all named exports.
BIt works fine.
CIt causes an error or undefined import.
DIt imports the file as a module object.
Which statement is true about named exports?
AYou can export multiple named components from one file.
BNamed exports must be imported without curly braces.
CYou can only have one named export per file.
DNamed exports are the same as default exports.
If a file exports a component as default, how do you rename it on import?
AUse <code>import NewName from './file';</code>
BYou cannot rename default exports.
CUse <code>import { NewName } from './file';</code>
DUse <code>export default NewName from './file';</code>
Explain how to export and import a React component using default export.
Think about how you share one main component from a file.
You got /3 concepts.
    Describe the difference between named exports and default exports in React components.
    Consider how you might organize many components in one file.
    You got /3 concepts.

      Practice

      (1/5)
      1. What is the main reason to export a React component from a file?
      easy
      A. To make the component run faster
      B. To hide the component from other files
      C. To use the component in other files by importing it
      D. To automatically style the component

      Solution

      1. Step 1: Understand the purpose of export

        Exporting a component allows it to be shared and used in other files.
      2. Step 2: Connect export with import usage

        Without export, other files cannot import and use the component.
      3. Final Answer:

        To use the component in other files by importing it -> Option C
      4. Quick Check:

        Export shares components [OK]
      Hint: Export lets you share components between files [OK]
      Common Mistakes:
      • Thinking export improves speed
      • Believing export hides components
      • Assuming export adds styles automatically
      2. Which of the following is the correct syntax to export a React component as the default export?
      easy
      A. export MyComponent = () =>
      Hello
      ;
      B. export default function MyComponent() { return
      Hello
      ; }
      C. default export MyComponent() { return
      Hello
      ; }
      D. export function default MyComponent() { return
      Hello
      ; }

      Solution

      1. Step 1: Recall default export syntax

        The correct syntax is 'export default' followed by the function declaration.
      2. Step 2: Check each option's syntax

        export default function MyComponent() { return
        Hello; } matches the correct syntax; others have invalid order or keywords.
      3. Final Answer:

        export default function MyComponent() { return <div>Hello</div>; } -> Option B
      4. Quick Check:

        Default export uses 'export default' [OK]
      Hint: Default export starts with 'export default' [OK]
      Common Mistakes:
      • Placing 'default' after function name
      • Using '=' with export incorrectly
      • Mixing 'export' and 'default' order
      3. Given these two files, what will be the output when 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>; }
      medium
      A. <div><button>Click</button></div>
      B. Error: Button not found
      C. <button>Click</button>
      D. <div>Button</div>

      Solution

      1. Step 1: Understand named export and import

        Button is exported as a named export and imported with curly braces correctly.
      2. Step 2: Analyze rendering in App component

        App renders a div containing the Button component, which returns a button element with text 'Click'.
      3. Final Answer:

        <div><button>Click</button></div> -> Option A
      4. Quick Check:

        Named export/import works = output includes button inside div [OK]
      Hint: Named exports import with braces { } [OK]
      Common Mistakes:
      • Importing named export without braces
      • Expecting Button text instead of button element
      • Confusing default and named exports
      4. What is wrong with this import statement?

      import Button from './Button';

      Given that Button.js contains:
      export function Button() { return <button>Click</button>; }
      medium
      A. Button is a named export but imported as default
      B. The file path is incorrect
      C. Button component is not exported at all
      D. The import statement is missing curly braces

      Solution

      1. Step 1: Check export type in Button.js

        Button is exported as a named export using 'export function Button'.
      2. Step 2: Compare with import syntax

        Importing without braces means default import, but Button is not default exported.
      3. Final Answer:

        Button is a named export but imported as default -> Option A
      4. Quick Check:

        Named export must import with braces [OK]
      Hint: Named exports need braces in import [OK]
      Common Mistakes:
      • Importing named export without braces
      • Assuming default export without checking
      • Ignoring export type in source file
      5. You have two components in 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?
      hard
      A. import WidgetA from './Widgets'; import WidgetB from './Widgets';
      B. import WidgetA, WidgetB from './Widgets';
      C. import * as Widgets from './Widgets';
      D. import { WidgetA, WidgetB } from './Widgets';

      Solution

      1. Step 1: Identify export type in Widgets.js

        Both WidgetA and WidgetB are named exports.
      2. Step 2: Choose correct import syntax for multiple named exports

        Use curly braces with both names inside: import { WidgetA, WidgetB } from './Widgets';
      3. Final Answer:

        import { WidgetA, WidgetB } from './Widgets'; -> Option D
      4. Quick Check:

        Multiple named exports import with braces and commas [OK]
      Hint: Import multiple named exports with { A, B } [OK]
      Common Mistakes:
      • Trying to import multiple named exports as default
      • Using separate default imports for each named export
      • Using import * without accessing properties
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard