0
0
Svelteframework~20 mins

Element reference bindings (bind:this) in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Element Reference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when using bind:this in Svelte?
Consider this Svelte snippet:
<script>
  let inputRef;
</script>

<input bind:this={inputRef} />

<button on:click={() => inputRef.focus()}>Focus Input</button>

What is the behavior when the button is clicked?
Svelte
<script>
  let inputRef;
</script>

<input bind:this={inputRef} />

<button on:click={() => inputRef.focus()}>Focus Input</button>
AThe button itself receives focus instead of the input.
BNothing happens because <code>inputRef</code> is undefined.
CAn error occurs because <code>bind:this</code> cannot be used on input elements.
DThe input element receives focus when the button is clicked.
Attempts:
2 left
💡 Hint
Remember that bind:this assigns the actual DOM element to the variable.
state_output
intermediate
2:00remaining
What is the value of the variable after mount with bind:this?
Given this Svelte code:
<script>
  import { onMount } from 'svelte';
  let divRef;
  let text = '';

  onMount(() => {
    text = divRef ? 'Element exists' : 'No element';
  });
</script>

<div bind:this={divRef}>Hello</div>
<p>{text}</p>

What will be displayed inside the <p> tag after the component mounts?
Svelte
<script>
  import { onMount } from 'svelte';
  let divRef;
  let text = '';

  onMount(() => {
    text = divRef ? 'Element exists' : 'No element';
  });
</script>

<div bind:this={divRef}>Hello</div>
<p>{text}</p>
AElement exists
BNo element
CAn empty string
DUndefined
Attempts:
2 left
💡 Hint
Think about when onMount runs and if divRef is assigned by then.
📝 Syntax
advanced
2:00remaining
Which option correctly uses bind:this to reference a component's DOM element?
You want to get a reference to a <section> element in your Svelte component. Which code snippet is correct?
A<section bind:this=sectionRef></section>
B<section bind:this={sectionRef}></section>
C<section bind:this="sectionRef"></section>
D<section bind:this={sectionRef}></section> with <code>let sectionRef = null;</code> missing
Attempts:
2 left
💡 Hint
Check the syntax for binding variables in Svelte directives.
🔧 Debug
advanced
2:00remaining
Why does this bind:this example cause an error?
Examine this Svelte code:
<script>
  let btnRef;
</script>

<button bind:this={btnRef} on:click={() => btnRef = null}>Click</button>

What error or problem will occur when clicking the button?
Svelte
<script>
  let btnRef;
</script>

<button bind:this={btnRef} on:click={() => btnRef = null}>Click</button>
AAfter clicking, <code>btnRef</code> becomes null, so future references cause errors.
BNo error; <code>btnRef</code> is correctly set to null and re-assigned automatically.
CSyntaxError because <code>btnRef</code> cannot be assigned inside event handler.
DThe button disappears from the DOM after clicking.
Attempts:
2 left
💡 Hint
Think about what happens when you overwrite the reference variable manually.
🧠 Conceptual
expert
3:00remaining
How does bind:this interact with component lifecycle and reactivity?
In Svelte, when you use bind:this to get a reference to a DOM element, which statement is true about its lifecycle and reactivity?
AThe variable bound with <code>bind:this</code> updates immediately when the element is created and is reactive to DOM changes.
BThe variable is assigned only once after the component mounts and does not update if the element is replaced.
CThe variable is reactive and updates automatically even if the element is removed and recreated in the DOM.
DThe variable is assigned after the component is destroyed.
Attempts:
2 left
💡 Hint
Consider what happens if the element with bind:this is conditionally rendered or replaced.