Consider these React components composed together. What will be rendered inside the <App /> component?
function Greeting({ name }) { return <p>Hello, {name}!</p>; } function Wrapper({ children }) { return <div className="wrapper">{children}</div>; } function App() { return ( <Wrapper> <Greeting name="Alice" /> </Wrapper> ); }
Remember that children props render the nested components inside the parent.
The Wrapper component wraps its children inside a div with class wrapper. The Greeting component renders a p tag with the greeting text. So the final output is a div with class wrapper containing the p tag.
Given these components, what is the displayed count after clicking the button once?
import React, { useState } from 'react'; function Counter({ count }) { return <p>Count: {count}</p>; } function Button({ onClick }) { return <button onClick={onClick}>Increment</button>; } function App() { const [count, setCount] = useState(0); return ( <> <Counter count={count} /> <Button onClick={() => setCount(count + 1)} /> </> ); }
Clicking the button triggers setCount to update the state.
The initial count is 0. Clicking the button calls setCount(count + 1), which updates count to 1. The Counter component receives the updated count and displays "Count: 1".
Which code correctly forwards all props from Wrapper to Inner component?
function Inner(props) { return <div>{props.message}</div>; } function Wrapper(props) { // Forward props to Inner return ??? }
Use spread syntax to forward all props.
Option D uses the spread operator {...props} to pass all props to Inner. Option D passes a single prop named props which is incorrect. Option D only passes message but not other props. Option D tries to spread a string which causes an error.
What error does this code cause and why?
function Parent() { return <Child />; } function Child(props) { return <div>{props.text}</div>; }
Check if props is declared in the function parameters.
The Child component uses props.text but does not declare props as a parameter. This causes a ReferenceError because props is undefined in the function scope.
Choose the best explanation for why React encourages component composition.
Think about how small pieces can build bigger things.
Component composition lets developers create complex user interfaces by assembling small, focused, reusable components. This improves code clarity and reuse. The other options are incorrect or false statements.