Performance: Spread props
Spread props affect how many attributes are added to DOM elements and how the browser processes them during rendering.
<Component id={props.id} class={props.class} aria-label={props['aria-label']} /><Component {...props} />| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Spread many props blindly | Many attribute insertions | Multiple reflows if styles affected | Higher paint cost due to larger DOM | [X] Bad |
| Explicitly pass needed props | Minimal attribute insertions | Single or no reflows | Lower paint cost with smaller DOM | [OK] Good |
{...props} do in a Svelte component?{...props} takes all key-value pairs from the props object.props object as individual props to the component. -> Option Auser as props to a Svelte component <Profile />?{...object} inside the component tag to pass all object keys as props./> without extra characters or parentheses.<Profile {...user} /> [OK] <script>
let data = {name: 'Alice', age: 30};
</script>
<UserCard {...data} age={25} />age inside UserCard?{...data} passes name: 'Alice' and age: 30 first.age={25} comes after spread and overrides the previous age value. <script>
let props = {title: 'Hello'};
</script>
<Card {...props} title />title without a value is treated as title={true} in Svelte.title='Hello' to true.title without value sets it to true. -> Option D<Button /> that accepts props type, disabled, and label. You want to pass an object btnProps with {type: 'submit', disabled: false} but override disabled to true and add label as 'Send'. Which is the correct usage?{...btnProps} to pass type and disabled: false.disabled and add label explicitlydisabled={true} and label='Send' must come after.<Button {...btnProps} disabled={true} label='Send' /> places spread first, then explicit props, ensuring correct override.Pass the test and this pattern moves unknown