0
0
NextJSframework~20 mins

Composition vs inheritance in NextJS - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Composition vs Inheritance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this Next.js component using composition?

Consider the following Next.js functional components using composition. What will be displayed on the page?

NextJS
function Button({ children }) {
  return <button>{children}</button>;
}

function Alert() {
  return <Button><strong>Warning!</strong> Check your input.</Button>;
}

export default function Page() {
  return <Alert />;
}
A<button>Warning! Check your input.</button>
B<button><strong>Warning!</strong> Check your input.</button>
C<strong>Warning!</strong> Check your input.
DError: children prop missing
Attempts:
2 left
💡 Hint

Look at how the children prop is passed and rendered inside the Button component.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes inheritance in React/Next.js?

In React and Next.js, what is a key reason developers prefer composition over inheritance?

AInheritance allows sharing code by extending components, but React discourages it because it complicates component reuse and flexibility.
BInheritance is the recommended way to share state between components in React and Next.js.
CInheritance automatically manages component lifecycle methods better than composition.
DInheritance is required to use hooks like <code>useState</code> and <code>useEffect</code>.
Attempts:
2 left
💡 Hint

Think about how React components are designed to be combined.

📝 Syntax
advanced
2:00remaining
Which Next.js component code correctly uses composition to wrap content with a layout?

Choose the option that correctly composes a Layout component wrapping page content.

A
function Layout({ content }) {
  return &lt;div className="layout"&gt;{content}&lt;/div&gt;;
}

export default function Page() {
  return &lt;Layout content={&lt;h1&gt;Home&lt;/h1&gt;} /&gt;;
}
B
function Layout() {
  return &lt;div className="layout"&gt;&lt;children /&gt;&lt;/div&gt;;
}

export default function Page() {
  return &lt;Layout&gt;&lt;h1&gt;Home&lt;/h1&gt;&lt;/Layout&gt;;
}
C
function Layout({ children }) {
  return &lt;div className="layout"&gt;{children}&lt;/div&gt;;
}

export default function Page() {
  return &lt;Layout&gt;&lt;h1&gt;Home&lt;/h1&gt;&lt;/Layout&gt;;
}
D
function Layout() {
  return &lt;div className="layout"&gt;{this.props.children}&lt;/div&gt;;
}

export default function Page() {
  return &lt;Layout&gt;&lt;h1&gt;Home&lt;/h1&gt;&lt;/Layout&gt;;
}
Attempts:
2 left
💡 Hint

Remember how functional components receive children as a prop.

🔧 Debug
advanced
2:30remaining
What is rendered by this Next.js component using inheritance?

Examine the code below. What will be rendered when this is used?

NextJS
import React from 'react';

class BaseComponent extends React.Component {
  render() {
    return <p>Base content</p>;
  }
}

class ChildComponent extends BaseComponent {
  render() {
    return <div>{super.render()} and child content</div>;
  }
}

export default function Page() {
  return <ChildComponent />;
}
AThe code is valid and will render: <div><p>Base content</p> and child content</div>.
BError: The ChildComponent must call super() in the constructor before using render().
CReact does not support class components extending other class components.
DError: Using super.render() inside JSX causes a React error because it returns a React element, which cannot be nested this way.
Attempts:
2 left
💡 Hint

Think about what super.render() returns and how JSX handles React elements as children.

state_output
expert
3:00remaining
What is the final count displayed after clicking the button twice in this Next.js component using composition?

Consider this Next.js functional component using composition and state. What number will be shown after clicking the button two times?

NextJS
import { useState } from 'react';

function CounterButton({ onClick }) {
  return <button onClick={onClick}>Increment</button>;
}

export default function Page() {
  const [count, setCount] = useState(0);

  function handleClick() {
    setCount(count + 1);
    setCount(count + 1);
  }

  return (
    <div>
      <p>Count: {count}</p>
      <CounterButton onClick={handleClick} />
    </div>
  );
}
ACount: 0
BCount: 2
CCount: 3
DCount: 1
Attempts:
2 left
💡 Hint

Remember how React batches state updates and how the current state value is captured in closures.