Performance: Reusable components
Reusable components affect rendering speed and bundle size by reducing duplicated code and minimizing unnecessary DOM updates.
function Button({ label, onClick }) { return <button onClick={onClick}>{label}</button>; } function App() { return ( <> <Button label="Click 1" onClick={() => alert('Clicked 1')} /> <Button label="Click 2" onClick={() => alert('Clicked 2')} /> <Button label="Click 3" onClick={() => alert('Clicked 3')} /> </> ); }
function App() { return ( <> <button onClick={() => alert('Clicked 1')}>Click 1</button> <button onClick={() => alert('Clicked 2')}>Click 2</button> <button onClick={() => alert('Clicked 3')}>Click 3</button> </> ); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Duplicated inline elements | Many unique nodes | Multiple reflows on updates | Higher paint cost due to many nodes | [X] Bad |
| Reusable components | Fewer unique nodes, reused | Single or fewer reflows | Lower paint cost due to reuse | [OK] Good |
title with value "Hello" to a reusable component Card?<Card title="Hello" /> uses correct syntax: title="Hello". <Card title={Hello} /> misses quotes around string. <Card title=Hello /> lacks quotes or braces. <Card "Hello"=title /> has incorrect attribute format.function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
<Greeting name="Alice" />
<Greeting name="Bob" /><Greeting /> renders an <h1> with the passed name prop.Hello, Alice!, second renders Hello, Bob!. Both outputs appear in order.function Button(props) {
return <button>Click {props.label}</button>
}
// Usage
<Button label=Submit /><Button label=Submit /> lacks quotes around "Submit" causing syntax error.List component that shows items with a custom renderItem prop function. Which approach correctly uses renderItem inside the component?map to transform each item into JSX, calling renderItem for each.function List(props) {
return <ul>{
props.items.map(item => <li>{props.renderItem(item)}</li>)
}</ul>;
} correctly maps items and calls renderItem inside <li>. function List(props) {
return <ul>{
props.items.map(props.renderItem(item))
}</ul>;
} calls renderItem incorrectly inside map. function List(props) {
return <ul>{
props.renderItem(props.items)
}</ul>;
} calls renderItem once with whole array. function List(props) {
return <ul>{
props.items.forEach(item => props.renderItem(item))
}</ul>;
} uses forEach which returns undefined, so no output.Pass the test and this pattern moves unknown