Performance: JSX vs HTML differences
This affects how the browser parses and renders UI elements, impacting initial load and update speed.
const element = <div className="container">Hello</div>; ReactDOM.render(element, root);
const htmlString = '<div class="container">Hello</div>'; ReactDOM.render(<div dangerouslySetInnerHTML={{__html: htmlString}} />, root);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Raw HTML string in React | Direct DOM insertion | Multiple reflows if content changes | High paint cost due to full DOM updates | [X] Bad |
| JSX with React.createElement | Virtual DOM diffing minimizes DOM ops | Single reflow on updates | Lower paint cost by updating only changed nodes | [OK] Good |
class is replaced by className because class is a reserved word in JavaScript.className.for is replaced by htmlFor because for is a reserved word in JavaScript.htmlFor with capital F, so <label htmlFor="inputId">Name</label> is correct.const element = <input type="checkbox" checked="true" />;
ReactDOM.render(element, document.getElementById('root'));checked="true" without curly braces sets the prop to the string "true".<input type="checkbox" checked="true" />. Browsers treat the presence of the checked attribute (any non-empty value) as checked for uncontrolled checkboxes.const element = <div class="container">Hello</div>;class. Instead, it uses className because class is a reserved word in JavaScript.style attribute expects a JavaScript object, so you use double curly braces: the outer for embedding JS, the inner for the object.fontWeight is correct. Values are strings like 'red' and 'bold'.Pass the test and this pattern moves unknown