0
0
Reactframework~20 mins

Exporting and importing components in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
React Export-Import Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this React component import?
Given two files, Header.js and App.js, where Header.js exports a component as export default function Header() { return <h1>Welcome</h1>; } and App.js imports it as import Header from './Header'; and renders <Header />, what will be displayed on the page?
React
/* Header.js */
export default function Header() {
  return <h1>Welcome</h1>;
}

/* App.js */
import React from 'react';
import Header from './Header';

export default function App() {
  return (
    <div>
      <Header />
    </div>
  );
}
AThe page shows the text 'Header' as plain text.
BThe page shows nothing because the import is incorrect.
CThe page shows a heading with the text 'Welcome'.
DThe page shows an error because Header is not a named export.
Attempts:
2 left
💡 Hint
Check how the component is exported and imported using default export.
📝 Syntax
intermediate
2:00remaining
Which import statement correctly imports a named export?
If a React component is exported as export function Footer() { return <footer>Bye</footer>; }, which import statement correctly imports it in another file?
React
export function Footer() {
  return <footer>Bye</footer>;
}
Aimport { Footer } from './Footer';
Bimport Footer from './Footer';
Cimport * as Footer from './Footer';
Dimport Footer() from './Footer';
Attempts:
2 left
💡 Hint
Named exports require curly braces in import.
🔧 Debug
advanced
2:00remaining
Why does this import cause an error?
Given export default function Nav() { return <nav>Menu</nav>; } in Nav.js, why does import { Nav } from './Nav'; cause an error?
React
/* Nav.js */
export default function Nav() {
  return <nav>Menu</nav>;
}

/* Another file */
import { Nav } from './Nav';

export default function App() {
  return <Nav />;
}
ABecause Nav component is missing a return statement.
BBecause Nav is a default export but imported as a named export.
CBecause the import path './Nav' is incorrect.
DBecause React components cannot be default exported.
Attempts:
2 left
💡 Hint
Check the difference between default and named exports and imports.
state_output
advanced
2:00remaining
What is the output when importing a component with a typo?
If a component is exported as export default function Sidebar() { return <aside>Links</aside>; } and imported as import SideBar from './Sidebar'; (note the uppercase B), what happens when rendering <SideBar />?
React
/* Sidebar.js */
export default function Sidebar() {
  return <aside>Links</aside>;
}

/* App.js */
import SideBar from './Sidebar';

export default function App() {
  return <SideBar />;
}
AThe code throws a runtime error because SideBar is undefined.
BThe component renders nothing because the import name is case-sensitive but matches the export.
CThe component renders nothing because the import name does not match the export name exactly.
DThe component renders correctly showing 'Links'.
Attempts:
2 left
💡 Hint
Default exports can be imported with any name regardless of case.
🧠 Conceptual
expert
2:00remaining
Which statement about exporting and importing React components is true?
Consider these statements about exporting and importing React components. Which one is true?
ANamed exports require curly braces when importing, and the imported name must match exactly.
BA component exported as default must be imported with the same name as the export function.
CYou can import a default export using curly braces if you rename it.
DYou cannot have both named and default exports in the same file.
Attempts:
2 left
💡 Hint
Think about how named exports and imports work in JavaScript modules.