0
0
Svelteframework~20 mins

beforeUpdate and afterUpdate in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Understanding beforeUpdate and afterUpdate execution order
Given the following Svelte component, what will be the order of console logs when the button is clicked once?
Svelte
  <script>
    import { beforeUpdate, afterUpdate } from 'svelte';
    let count = 0;

    beforeUpdate(() => {
      console.log('Before update:', count);
    });

    afterUpdate(() => {
      console.log('After update:', count);
    });

    function increment() {
      count += 1;
    }
  </script>

  <button on:click={increment}>Increment</button>
  <p>{count}</p>
A1,2
B2,1
C1
D2
Attempts:
2 left
💡 Hint
Remember that beforeUpdate runs before the DOM updates and afterUpdate runs after.
state_output
intermediate
2:00remaining
What is the value of message after update?
Consider this Svelte component. What will be the final value of message after the button is clicked once?
Svelte
  <script>
    import { beforeUpdate, afterUpdate } from 'svelte';
    let message = 'start';

    beforeUpdate(() => {
      message = 'before';
    });

    afterUpdate(() => {
      console.log('after');
    });

    function change() {
      message = 'changed';
    }
  </script>

  <button on:click={change}>Change</button>
  <p>{message}</p>
A"after"
B"before"
C"changed"
D"start"
Attempts:
2 left
💡 Hint
Think about the order of lifecycle hooks and how they modify message.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in lifecycle hook usage
Which option contains a syntax error in using beforeUpdate or afterUpdate in Svelte?
AbeforeUpdate { () => console.log('update') };
BafterUpdate(() => console.log('update'));
CbeforeUpdate(() => { console.log('update'); });
DafterUpdate(() => { console.log('update'); });
Attempts:
2 left
💡 Hint
Check the parentheses and arrow function syntax carefully.
🔧 Debug
advanced
2:00remaining
Why does afterUpdate cause an infinite loop here?
Examine this Svelte code snippet. Why does it cause an infinite update loop?
Svelte
  <script>
    import { afterUpdate } from 'svelte';
    let count = 0;

    afterUpdate(() => {
      count += 1;
    });
  </script>

  <p>{count}</p>
ABecause <code>afterUpdate</code> is not imported correctly.
BBecause <code>count</code> is not initialized.
CBecause <code>count</code> is not displayed in the template.
DBecause <code>afterUpdate</code> changes <code>count</code>, triggering itself repeatedly.
Attempts:
2 left
💡 Hint
Think about what triggers Svelte to run afterUpdate again.
🧠 Conceptual
expert
3:00remaining
Choosing the correct lifecycle hook for DOM measurement
You want to measure the size of a DOM element after it updates in Svelte. Which lifecycle hook should you use and why?
A<code>beforeUpdate</code>, because it runs before the DOM changes and lets you measure the old size.
BNeither, you should use <code>onMount</code> because it runs once after component loads.
C<code>afterUpdate</code>, because it runs after the DOM updates and lets you measure the new size.
D<code>beforeUpdate</code>, because it runs after the DOM updates and lets you measure the new size.
Attempts:
2 left
💡 Hint
Think about when the DOM reflects the latest changes.