Imagine you’re building a simple React app for a blog website. You write the following JSX code inside a component:
function BlogPost() { const title = "My First Post"; return ( <div> <h1>{title}</h1> <p>Welcome to my blog!</p> </div> ); }
What is true about this code?
The <div> tag demonstrates the use of a React fragment.
<div>
JSX requires all tags to be self-closing, so <p> should be <p />.
<p>
<p />
The component can be reused with different titles if title were passed as a prop instead of hard-coded.
title
This code will throw an error because title isn’t defined properly.
The {title} syntax is an example of a JavaScript expression being used in JSX.
{title}
You’re tasked with adding a button to a React component that logs "Clicked!" to the console when pressed. Here’s the code:
function Button() { function handleClick() { console.log("Clicked!"); } return <button onClick={handleClick}>Press Me</button>; }
What can you say about the event handling in this code?
The onClick={handleClick} syntax correctly binds the handleClick function to the button’s click event.
onClick={handleClick}
handleClick
If you wrote onClick={console.log("Clicked!")} instead, it would log "Clicked!" every time the component renders, not just on click.
onClick={console.log("Clicked!")}
Event handlers like handleClick must always return a value to work properly in React.
The code will crash because handleClick should be written as onClick={handleClick()} to execute immediately.
onClick={handleClick()}
You’re refactoring a component that renders a list of items. The original code uses a <div> wrapper:
function ItemList() { return ( <div> <li>Item 1</li> <li>Item 2</li> </div> ); }
You decide to use a React fragment instead. Which of these options correctly replaces the <div> with a fragment?
<div fragment> <li>Item 1</li> <li>Item 2</li> </div>
<Fragment> <li>Item 1</li> <li>Item 2</li> </Fragment>
{ <li>Item 1</li> <li>Item 2</li> }
<> <li>Item 1</li> <li>Item 2</li> </>
You’re designing a greeting component for a website. Here’s the code:
function Greeting(props) { const name = props.name; return <h1>Hello, {name}!</h1>; }
A junior developer reviews it and suggests changes, such as adding interactivity when the user clicks the greeting or improving the component's memory footprint. Which of these suggestions would improve or maintain the component’s functionality?
Add an event handler like onClick={() => alert("Hi!")} to the <h1> to make it interactive.
onClick={() => alert("Hi!")}
<h1>
Replace {name} with {props.name} directly to avoid an extra variable.
{name}
{props.name}
Wrap the <h1> in a <div> because JSX requires a single parent element.
Change {name} to {name()} because expressions in JSX must be functions.
{name()}
Suppose you’re building a counter app with this code:
function Counter() { let count = 0; function increment() { count += 1; console.log(count); } return ( <> <p>Count: {count}</p> <button onClick={increment}>Add 1</button> </> ); }
You test it, click the button, and notice something odd. What’s happening, and why?
The UI updates correctly, but the console lags because console.log is asynchronous.
console.log
The code will throw an error because count isn’t declared with const.
count
const
The <> fragment causes the button to break, so clicks don’t register.
<>
The count increases in the console but the UI doesn’t update because count isn’t managed by React’s state.
This content is only available to registered users.Please register at the the home page.