Consider the following Next.js functional components using composition. What will be displayed on the page?
function Button({ children }) { return <button>{children}</button>; } function Alert() { return <Button><strong>Warning!</strong> Check your input.</Button>; } export default function Page() { return <Alert />; }
Look at how the children prop is passed and rendered inside the Button component.
The Alert component uses composition by passing JSX as children to the Button component. The Button renders its children inside a <button> element, so the output includes the <strong> tag inside the button.
In React and Next.js, what is a key reason developers prefer composition over inheritance?
Think about how React components are designed to be combined.
React encourages composition because it is simpler and more flexible than inheritance. Inheritance can make components tightly coupled and harder to reuse. Hooks and functional components work best with composition.
Choose the option that correctly composes a Layout component wrapping page content.
Remember how functional components receive children as a prop.
Option C correctly uses the children prop to wrap content. Option C tries to render <children /> which is invalid JSX. Option C uses a different prop name but works, but the question asks for composition with children. Option C uses this.props which is invalid in functional components.
Examine the code below. What will be rendered when this is used?
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 />; }
Think about what super.render() returns and how JSX handles React elements as children.
The code renders without error. super.render() returns a React element from the base class, which is validly nested inside the child <div>. However, React strongly discourages inheritance for sharing behavior between components, favoring composition instead, as inheritance often leads to inflexible and hard-to-maintain code.
Consider this Next.js functional component using composition and state. What number will be shown after clicking the button two times?
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> ); }
Remember how React batches state updates and how the current state value is captured in closures.
Each button click calls handleClick() once. Inside handleClick, the two setCount(count + 1) calls capture the same count value (stale closure) and are batched by React, resulting in only one increment per click. After two clicks, the count is incremented twice, displaying 2.