Performance: Element reference bindings (bind:this)
This affects how quickly the browser can access and manipulate DOM elements after initial render, impacting interaction responsiveness.
let inputElement; function focusInput() { inputElement.focus(); } <input bind:this={inputElement} /> <button on:click={focusInput}>Focus</button>
let inputElement; function focusInput() { inputElement = document.querySelector('#myInput'); inputElement.focus(); } <input id="myInput" /> <button on:click={focusInput}>Focus</button>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using document.querySelector repeatedly | Multiple DOM queries | Triggers 1 reflow per query | Moderate paint cost due to layout recalculation | [X] Bad |
| Using bind:this once to store element reference | Single DOM reference assignment | No reflows on access | Minimal paint cost | [OK] Good |
bind:this do in a Svelte component?bind:thisbind:this is used to get a direct reference to an HTML element inside a Svelte component.bind:this links a variable to the actual DOM element, allowing direct manipulation.bind:this = direct element reference [OK]bind:this links variable to element itself [OK]bind:this with binding to state variablesmyButton to a button element using bind:this in Svelte?bind:this requires curly braces around the variable name: bind:this={variable}.<button bind:this={myButton}>Click me</button> uses correct syntax with curly braces. Options A, B, and C use incorrect or invalid syntax.bind:this [OK]bind:this [OK] <script>
let inputRef;
function focusInput() {
inputRef.focus();
}
</script>
<input bind:this={inputRef} placeholder="Type here"/>
<button on:click={focusInput}>Focus Input</button>bind:this usageinputRef variable is bound to the input element, so it holds the actual input DOM element.focusInput functioninputRef.focus() calls the native focus method on the input element, placing the cursor inside it.bind:this gives element to call focus() [OK]bind:this gives element to call methods on [OK]inputRef is undefinedfocus() is invalid <script>
let box;
</script>
<div bind:this=box>Content</div>
<button on:click={() => console.log(box)}>Log Box</button>bind:this syntaxbind:this must be inside curly braces: bind:this={box}.div is valid, box can be let, and the event handler syntax is correct.bind:this syntax is missing curly braces around box. -> Option Dbind:this [OK]bind:this [OK]bind:thisbind:this can't be used on divdiv element after it renders in Svelte. Which approach correctly uses bind:this and lifecycle to get the width?box element is only available after the component mounts, so measuring width must happen inside onMount.<script>
import { onMount } from 'svelte';
let box;
let width = 0;
onMount(() => {
width = box.clientWidth;
});
</script>
<div bind:this={box}>Content</div>
<p>Width: {width}px</p> uses onMount to safely access box.clientWidth. Options B and C try to access width before box is set, causing errors. <script>
import { beforeUpdate } from 'svelte';
let box;
let width = 0;
beforeUpdate(() => {
width = box.clientWidth;
});
</script>
<div bind:this={box}>Content</div>
<p>Width: {width}px</p> uses beforeUpdate, which runs before DOM updates, so box may not be ready.bind:this with onMount to measure width. -> Option ConMount after binding [OK]onMount to access element size after render [OK]beforeUpdate instead of onMountbind:this to get element referencePass the test and this pattern moves unknown