0
0
Reactframework~20 mins

Component organization in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
React Component Organizer
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?

Consider this React component structure. What will be rendered on the screen?

React
import React from 'react';

function Header() {
  return <header><h1>Welcome</h1></header>;
}

function Footer() {
  return <footer><p>Goodbye</p></footer>;
}

function Page() {
  return (
    <main>
      <Header />
      <section><p>Main content</p></section>
      <Footer />
    </main>
  );
}

export default Page;
A<header><h1>Welcome</h1></header><section><p>Main content</p></section><footer><p>Goodbye</p></footer>
B<main><section><p>Main content</p></section></main>
C<main><header><h1>Welcome</h1></header><section><p>Main content</p></section><footer><p>Goodbye</p></footer></main>
D<header><h1>Welcome</h1></header><main><section><p>Main content</p></section></main><footer><p>Goodbye</p></footer>
Attempts:
2 left
💡 Hint

Look at how components are nested inside the Page component's return statement.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a reusable button component in React?

Which code snippet correctly defines a reusable button component that accepts a label prop and calls onClick when clicked?

A
function Button({ label, onClick }) {
  return &lt;button onClick={onClick}&gt;{label}&lt;/button&gt;;
}
B
function Button(label, onClick) {
  return &lt;button onClick={onClick}&gt;{label}&lt;/button&gt;;
}
C
const Button = (props) =&gt; {
  return &lt;button&gt;{props.label}&lt;/button&gt;;
}
D
function Button({ label }) {
  return &lt;button onClick={onClick}&gt;{label}&lt;/button&gt;;
}
Attempts:
2 left
💡 Hint

Remember that props are passed as an object and event handlers must be assigned properly.

🔧 Debug
advanced
2:00remaining
Why does this React component fail to update the displayed count?

Examine the code below. Why does clicking the button not update the displayed count?

React
import React, { useState } from 'react';

function Counter() {
  let count = 0;

  function increment() {
    count += 1;
  }

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Add</button>
    </div>
  );
}

export default Counter;
ABecause the button element is missing an onClick attribute.
BBecause the increment function is not called when the button is clicked.
CBecause useState is used incorrectly and should be replaced with useEffect.
DBecause 'count' is a local variable and React does not track changes to it for re-rendering.
Attempts:
2 left
💡 Hint

Think about how React tracks changes to update the UI.

lifecycle
advanced
2:00remaining
What happens when this React component uses useEffect with an empty dependency array?

Consider this component:

import React, { useEffect } from 'react';

function Logger() {
  useEffect(() => {
    console.log('Component mounted');
  }, []);

  return <div>Check console</div>;
}

export default Logger;

What is the behavior of the console log?

AThe message 'Component mounted' logs only once when the component first appears.
BThe message never logs because the dependency array is empty.
CThe message logs every time the component re-renders.
DThe message logs repeatedly in an infinite loop.
Attempts:
2 left
💡 Hint

Recall how useEffect behaves with an empty dependency array.

🧠 Conceptual
expert
2:00remaining
How does splitting a large React component into smaller components improve the app?

Which of the following is the best explanation for why organizing UI into smaller React components is beneficial?

AIt forces all state to be managed globally, simplifying data flow.
BIt improves code reuse, readability, and makes testing and maintenance easier.
CIt reduces the total number of components, making the app faster to load.
DIt eliminates the need for props by keeping all logic in one place.
Attempts:
2 left
💡 Hint

Think about how smaller parts help manage complexity.