Performance: Declaring props with export let
This affects the initial rendering speed and update responsiveness of Svelte components by controlling how data is passed and tracked.
<script> export let data; </script> <!-- Parent passes data directly as prop --> <Child {data} />
<script> let data; let childRef; </script> <!-- Parent manually updates child via DOM or events --> <Child bind:this={childRef} /> <!-- Parent updates child by calling methods or manipulating DOM directly -->
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual DOM updates or event calls for passing data | High - multiple direct DOM manipulations | Multiple reflows per update | High paint cost due to many changes | [X] Bad |
| Using export let props for data passing | Low - reactive updates only | Single reflow per update | Low paint cost with minimal changes | [OK] Good |
export let do inside a Svelte component's <script> block?export letexport let declares a property (prop) that the parent component can pass in.export let specifically exposes variables as props for customization.title with a default value of "Hello" in Svelte?export let followed by the variable name and optional default value.export let title = "Hello"; matches the correct syntax: export let title = "Hello";. Others have wrong order or keywords.<script>
export let count = 5;
</script>
<p>Count is {count}</p><MyComponent count={10} />?count of 5, but the parent passes 10, which overrides the default.<script>
export let name;
name = "John";
</script>
<p>Hello {name}</p>export let should not be reassigned inside the component; they come from outside.name = "John" after declaration, which is not allowed and causes an error.items which is an array, but if no prop is passed, it should default to an empty array. Which code correctly declares this prop?export let.export let items = []; correctly declares items with default empty array. export let items;if (!items) items = []; tries to assign after declaration, which is not allowed. let items = []; is local variable, not prop. export const items = []; uses const which is incorrect for props.Pass the test and this pattern moves unknown