Consider this React component structure. What will be rendered on the screen?
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;
Look at how components are nested inside the Page component's return statement.
The Page component returns a <main> element that contains <Header />, a <section>, and <Footer />. So the output is the <main> wrapping all three parts.
Which code snippet correctly defines a reusable button component that accepts a label prop and calls onClick when clicked?
Remember that props are passed as an object and event handlers must be assigned properly.
Option A correctly destructures label and onClick from props and assigns onClick to the button. Option A does not destructure props properly. Option A misses the onClick handler. Option A uses onClick without receiving it as a prop.
Examine the code below. Why does clicking the button not update the displayed count?
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;
Think about how React tracks changes to update the UI.
React only re-renders components when state or props change. Here, 'count' is a normal variable, so React does not know it changed. Using useState is needed to trigger re-render.
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?
Recall how useEffect behaves with an empty dependency array.
When the dependency array is empty, useEffect runs only once after the first render, similar to componentDidMount.
Which of the following is the best explanation for why organizing UI into smaller React components is beneficial?
Think about how smaller parts help manage complexity.
Breaking UI into smaller components helps reuse code, makes it easier to read and test, and simplifies maintenance. It does not reduce components or force global state.