Bird
Raised Fist0
Vueframework~20 mins

Custom refs with customRef in Vue - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Custom Ref Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Vue component using customRef?
Consider this Vue 3 component using customRef to debounce input updates. What will be logged when the user types 'abc' quickly?
Vue
import { defineComponent, customRef } from 'vue';

export default defineComponent({
  setup() {
    const debounced = customRef((track, trigger) => {
      let timeout;
      let value = '';
      return {
        get() {
          track();
          return value;
        },
        set(newValue) {
          clearTimeout(timeout);
          timeout = setTimeout(() => {
            value = newValue;
            trigger();
            console.log('Value updated:', value);
          }, 200);
        }
      };
    });

    return { debounced };
  }
});
ALogs only 'Value updated: abc' once, 200ms after the last input
BLogs 'Value updated: a' immediately, then 'Value updated: abc' after 200ms
CLogs nothing because the debounce never triggers
DLogs 'Value updated: a', then 'Value updated: ab', then 'Value updated: abc' after 200ms each
Attempts:
2 left
💡 Hint
Think about how debounce delays the update until typing stops.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates a customRef that logs on get and set?
Select the code snippet that correctly implements a customRef which logs 'Getting value' on get and 'Setting value' on set.
A
customRef((track, trigger) => {
  let value = 0;
  return {
    get() {
      track();
      console.log('Getting value');
      return value;
    },
    set(newVal) {
      value = newVal;
      trigger();
      console.log('Setting value');
    }
  };
})
B
customRef((track, trigger) => {
  let value = 0;
  return {
    get() {
      console.log('Getting value');
      track();
      return value;
    },
    set(newVal) {
      console.log('Setting value');
      value = newVal;
      trigger();
    }
  };
})
C
customRef((track, trigger) => {
  let value = 0;
  return {
    get() {
      track();
      return value;
      console.log('Getting value');
    },
    set(newVal) {
      value = newVal;
      trigger();
      console.log('Setting value');
    }
  };
})
D
customRef((track, trigger) => {
  let value = 0;
  return {
    get() {
      console.log('Getting value');
      return value;
      track();
    },
    set(newVal) {
      console.log('Setting value');
      trigger();
      value = newVal;
    }
  };
})
Attempts:
2 left
💡 Hint
Remember that track() must be called before returning the value in get, and trigger() after setting the value.
🔧 Debug
advanced
2:00remaining
Why does this customRef not trigger updates correctly?
This customRef is intended to debounce updates but Vue components don't react to changes. What is the problem?
Vue
const debounced = customRef((track, trigger) => {
  let value = '';
  let timeout = null;
  return {
    get() {
      track();
      return value;
    },
    set(newValue) {
      clearTimeout(timeout);
      timeout = setTimeout(() => {
        value = newValue;
        // Missing trigger call here
        console.log('Updated:', value);
      }, 300);
    }
  };
});
AThe value variable should be reactive using ref() inside customRef.
BThe track() function is missing in the get method, so reactivity is broken.
CThe trigger() function is not called after setting the value, so Vue does not know to update.
DThe timeout variable should be declared inside the set method, not outside.
Attempts:
2 left
💡 Hint
Check if Vue is notified when the value changes.
❓ state_output
advanced
2:00remaining
What is the value of count after these operations with a customRef?
Given this customRef that doubles the value on set, what is the final value of count after these lines run?
count.value = 2;
count.value = 3;
Vue
import { customRef } from 'vue';

const count = customRef((track, trigger) => {
  let value = 0;
  return {
    get() {
      track();
      return value;
    },
    set(newVal) {
      value = newVal * 2;
      trigger();
    }
  };
});
A6
B3
C4
D0
Attempts:
2 left
💡 Hint
Remember the set multiplies the input by 2 before storing.
🧠 Conceptual
expert
2:00remaining
Which statement about Vue's customRef is true?
Choose the correct statement about customRef in Vue 3.
AcustomRef can only be used inside template expressions.
BcustomRef automatically makes the value deeply reactive without extra code.
CcustomRef replaces the need for computed properties in all cases.
DcustomRef allows you to customize how Vue tracks and triggers reactivity for a ref's value.
Attempts:
2 left
💡 Hint
Think about what customRef controls in Vue's reactivity.

Practice

(1/5)
1. What is the main purpose of using customRef in Vue?
easy
A. To create reactive references with custom get and set behavior
B. To define a component's template
C. To register global components
D. To handle lifecycle hooks automatically

Solution

  1. Step 1: Understand what customRef does

    customRef allows you to create reactive references where you control how the value is read and written.
  2. Step 2: Compare with other options

    Options A, C, and D relate to other Vue features, not customRef.
  3. Final Answer:

    To create reactive references with custom get and set behavior -> Option A
  4. Quick Check:

    customRef = custom reactive behavior [OK]
Hint: Remember: customRef customizes reactive get/set [OK]
Common Mistakes:
  • Confusing customRef with component registration
  • Thinking customRef manages lifecycle hooks
  • Assuming customRef defines templates
2. Which of the following is the correct way to import customRef in a Vue 3 component?
easy
A. import { CustomRef } from 'vue';
B. import customRef from 'vue';
C. import { customRef } from 'vue';
D. import * as customRef from 'vue';

Solution

  1. Step 1: Recall Vue 3 import syntax

    Vue exports customRef as a named export, so it must be imported with curly braces.
  2. Step 2: Check each option

    import { customRef } from 'vue'; uses correct named import syntax. import customRef from 'vue'; tries default import which is incorrect. import { CustomRef } from 'vue'; uses wrong capitalization. import * as customRef from 'vue'; imports everything as an object, which is not typical for this usage.
  3. Final Answer:

    import { customRef } from 'vue'; -> Option C
  4. Quick Check:

    Named import uses curly braces [OK]
Hint: Use curly braces for named imports like customRef [OK]
Common Mistakes:
  • Using default import instead of named import
  • Wrong capitalization of customRef
  • Importing everything instead of named import
3. Given the following code, what will be logged when myRef.value = 10 is called?
import { customRef } from 'vue';

const myRef = customRef((track, trigger) => {
  let value = 0;
  return {
    get() {
      track();
      return value;
    },
    set(newValue) {
      if (newValue !== value) {
        value = newValue;
        trigger();
        console.log('Value updated');
      }
    }
  };
});

myRef.value = 10;
medium
A. Value updated
B. No output
C. Error: track is not defined
D. Value updated twice

Solution

  1. Step 1: Understand the set function behavior

    The set function checks if the new value differs from the current one. Since 10 !== 0, it updates value, triggers reactivity, and logs 'Value updated'.
  2. Step 2: Confirm no repeated logs or errors

    There is no repeated assignment or error in the code, so only one log occurs.
  3. Final Answer:

    Value updated -> Option A
  4. Quick Check:

    Set logs once on value change [OK]
Hint: Set logs only when value changes [OK]
Common Mistakes:
  • Expecting no output because of track confusion
  • Thinking trigger causes multiple logs
  • Assuming error due to track usage
4. Identify the error in this customRef implementation:
import { customRef } from 'vue';

const myRef = customRef((track, trigger) => {
  let value = 0;
  return {
    get() {
      return value;
    },
    set(newValue) {
      value = newValue;
      trigger();
    }
  };
});
medium
A. customRef cannot be used with arrow functions
B. Missing call to trigger() inside get()
C. Value should be declared with ref()
D. Missing call to track() inside get()

Solution

  1. Step 1: Check get() method for reactivity tracking

    The get() method must call track() to register dependencies. Here, it returns value without calling track(), so reactivity won't work properly.
  2. Step 2: Verify other parts

    Trigger is correctly called in set(). Value does not need to be a ref inside customRef. Arrow functions are allowed.
  3. Final Answer:

    Missing call to track() inside get() -> Option D
  4. Quick Check:

    track() needed in get() for reactivity [OK]
Hint: Always call track() inside get() [OK]
Common Mistakes:
  • Forgetting to call track() in get()
  • Thinking trigger() is needed in get()
  • Assuming value must be a ref inside customRef
5. You want to create a customRef that debounces updates by 300ms before triggering reactivity. Which code snippet correctly implements this behavior?
hard
A. const debouncedRef = customRef((track, trigger) => { let value; return { get() { track(); return value; }, set(newValue) { value = newValue; trigger(); } }; });
B. const debouncedRef = customRef((track, trigger) => { let value; let timeout; return { get() { track(); return value; }, set(newValue) { clearTimeout(timeout); timeout = setTimeout(() => { value = newValue; trigger(); }, 300); } }; });
C. const debouncedRef = customRef((track, trigger) => { let value; let timeout; return { get() { return value; }, set(newValue) { clearTimeout(timeout); timeout = setTimeout(() => { value = newValue; trigger(); }, 300); } }; });
D. const debouncedRef = customRef((track, trigger) => { let value; let timeout; return { get() { track(); return value; }, set(newValue) { value = newValue; clearTimeout(timeout); timeout = setTimeout(() => { trigger(); }, 300); } }; });

Solution

  1. Step 1: Understand debounce logic in set()

    Debounce means delaying the update and trigger until 300ms after the last set call. The correct code clears previous timeout, sets new timeout to update value and trigger after 300ms.
  2. Step 2: Check get() calls track()

    The correct code calls track() in get() to register dependencies. Missing track() in get() breaks reactivity. The remaining options either lack track(), do not debounce properly, or have incorrect debouncing logic.
  3. Final Answer:

    Correctly implements debounced updates with track() in get() -> Option B
  4. Quick Check:

    Debounce needs clearTimeout + delayed trigger [OK]
Hint: Debounce by delaying trigger with setTimeout and clearTimeout [OK]
Common Mistakes:
  • Not calling track() inside get()
  • Triggering immediately without debounce
  • Setting value outside timeout causing immediate update
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard