import React from 'react'; export default function Greeting() { const name = 'Alex'; return <h1>Hello, {name}!</h1>; }
The component uses curly braces to insert the value of the name variable inside the JSX. So it shows 'Hello, Alex!'.
Welcome that returns a <div> with text 'Welcome!'.Option A uses an arrow function with implicit return of JSX, which is correct. Option A is valid JavaScript but lacks export or const, so less preferred. Options B and D miss return statements causing errors.
import React, { useState } from 'react'; export default function Counter() { const [count, setCount] = useState(0); function increment() { setCount(count + 1); setCount(count + 1); } return ( <div> <p>Count: {count}</p> <button onClick={increment}>Add 2</button> </div> ); }
In the increment function, both setCount(count + 1) calls capture the same current count value due to closure and batching. Thus, each button click increases the count by 1 only. After two clicks, the count is 2.
import React from 'react'; export default function List() { const items = ['a', 'b', 'c']; return ( <ul> {items.map(item => <li>{item}</li>)} </ul> ); }
React needs a unique key prop on each list item to identify elements during updates. Missing keys cause warnings.
if (someCondition) {
const [value, setValue] = useState(0);
}
What is the result of calling a hook conditionally like this?React hooks must be called in the same order on every render. Calling them conditionally breaks this rule and causes errors.