Complete the code to create a simple component using composition in Next.js.
function Button({ label }) {
return <button>{label}</button>;
}
function App() {
return <div>[1] label="Click me" />;</div>;
}The App component uses composition by including the Button component inside its JSX.
Complete the code to extend a component using inheritance in React (Next.js).
import React from 'react'; class BaseButton extends React.Component { render() { return <button>{this.props.label}</button>; } } class [1] extends BaseButton { render() { return <button style={{ color: 'red' }}>{this.props.label}</button>; } }
The RedButton class extends BaseButton to change the button style using inheritance.
Fix the error in the code by completing the blank to correctly use composition with props.
function Card({ title, content }) {
return (
<div>
<h2>{title}</h2>
<p>{content}</p>
</div>
);
}
function Page() {
const info = { title: 'Hello', content: 'Welcome to the page' };
return <Card [1] />;
}Using the spread operator ...info passes all properties of info as props to Card.
Fill both blanks to create a component that uses composition and passes a callback prop.
function Button({ onClick, label }) {
return <button onClick=[1]>{label}</button>;
}
function App() {
function handleClick() {
alert('Clicked!');
}
return <Button onClick=[2] label="Press me" />;
}The onClick prop should be a function reference, so both blanks use handleClick without calling it.
Fill all three blanks to create a component that uses inheritance and overrides a method with additional behavior.
import React from 'react'; class Logger extends React.Component { log() { console.log('Logging'); } render() { return <div>Logger</div>; } } class CustomLogger extends Logger { [1]() { super.[2](); console.log([3]); } }
The CustomLogger class overrides the log method, calls the original with super.log(), and adds a custom message.