Performance: JSX syntax rules
JSX syntax affects how React components are parsed and rendered, impacting initial load and rendering speed.
const MyComponent = () => { return <div><p>Text</p></div>; };
const MyComponent = () => { return <div><p>Text</p></div>; };
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Incorrect JSX syntax causing errors | 0 (no DOM created) | 0 (render blocked) | 0 (no paint) | [X] Bad |
| JSX with multiple root elements without fragment | 0 (error blocks DOM) | 0 | 0 | [X] Bad |
| Correct JSX with fragments | Minimal DOM nodes | 1 reflow per update | Low paint cost | [OK] Good |
| Correct JSX with proper expressions | Efficient DOM updates | Minimal reflows | Low paint cost | [OK] Good |
<h1>Hello, {name}!</h1> correctly uses curly braces around the variable name. The other options use invalid syntax for embedding expressions.<h1>Hello, {name}!</h1> [OK]input, img, and br must be self-closed with a slash /.<input type='text' /> correctly self-closes input. <div><img src='logo.png'></div> misses self-closing on img. <p>Hello<p> has an unclosed p tag. <br> misses self-closing on br.<input type='text' /> [OK]const name = 'Alice';
const element = <div>Hello, {name.toUpperCase()}!</div>;{name.toUpperCase()} calls the toUpperCase() method on the string 'Alice', resulting in 'ALICE'.<div>Hello, ALICE!</div>.<div>Hello, ALICE!</div> [OK]const element = <div> <p>Welcome</p> <p>User</p> </div>; const wrongElement = <p>Hello</p> <p>World</p>;
div wrapping two p tags, which is correct. The second snippet has two sibling p tags without a parent wrapper.const names = ['Anna', 'Bob', 'Cara'];
A) <ul>{names.map(name => <li>{name}</li>)}</ul>
B) <ul>{names.forEach(name => <li>{name}</li>)}</ul>
C) <ul>{names.map(name => <li>name</li>)}</ul>
D) <ul>{names.map(name => <li>{name.toUpperCase()}</li>)}</ul>map to transform array items into JSX elements. Each item must be embedded inside curly braces.{names.map(name => <li>{name}</li>)} correctly uses map and embeds {name}. The forEach snippet returns undefined, so no list items render. The snippet <li>name</li> renders the literal string 'name' because it lacks curly braces. The toUpperCase() snippet uses valid JSX syntax but transforms names to uppercase, which is not the original list.Pass the test and this pattern moves unknown