Bird
Raised Fist0
Vueframework~20 mins

Reactive objects with reactive in Vue - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Vue Reactive Master
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 reactive object update?
Consider the following Vue 3 code using reactive. What will be logged to the console after the update?
Vue
import { reactive } from 'vue';

const state = reactive({ count: 0 });

console.log(state.count);
state.count = 5;
console.log(state.count);
A0 then 0
B0 then 5
Cundefined then 5
D5 then 5
Attempts:
2 left
💡 Hint
Remember that reactive objects track changes and reflect updates immediately.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates a reactive nested object in Vue 3?
You want to create a reactive object with a nested property user.name. Which code snippet correctly does this?
Aconst state = reactive({ user: { name: ref('Alice') } });
Bconst state = reactive({ user: reactive({ name: 'Alice' }) });
Cconst state = reactive({ user: ref({ name: 'Alice' }) });
Dconst state = reactive({ user: { name: 'Alice' } });
Attempts:
2 left
💡 Hint
Nested objects inside reactive are automatically reactive.
🔧 Debug
advanced
2:00remaining
Why does this reactive object update not trigger reactivity?
Given this code, why does updating state.user.age not update the UI?
Vue
import { reactive } from 'vue';

const state = reactive({});

state.user = {};

// Later in code
state.user.age = 30;
ABecause <code>age</code> was not present when <code>state.user</code> was made reactive, Vue cannot track it.
BBecause <code>reactive</code> only tracks top-level properties, nested properties are ignored.
CBecause <code>state.user</code> is not reactive, only <code>state</code> is reactive.
DBecause <code>age</code> must be wrapped in <code>ref</code> to be reactive.
Attempts:
2 left
💡 Hint
Vue 3's reactivity system cannot detect new properties added after reactive creation.
❓ state_output
advanced
2:00remaining
What is the value of state.count after this code runs?
Analyze this Vue 3 reactive code and determine the final value of state.count:
Vue
import { reactive } from 'vue';

const state = reactive({ count: 1 });

function increment() {
  state.count += 1;
}

increment();
increment();
state.count = 10;
A10
B3
C2
Dundefined
Attempts:
2 left
💡 Hint
The last assignment overwrites previous increments.
🧠 Conceptual
expert
3:00remaining
Which statement best describes Vue 3's reactive behavior with nested objects?
Select the most accurate description of how Vue 3's reactive function handles nested objects and property additions.
AVue 3's <code>reactive</code> makes nested objects reactive only if they exist at creation; new nested properties added later are not reactive.
BVue 3's <code>reactive</code> automatically tracks all nested objects and any new properties added at any time.
CVue 3's <code>reactive</code> only tracks top-level properties; nested objects must be wrapped separately with <code>reactive</code>.
DVue 3's <code>reactive</code> requires all nested properties to be wrapped with <code>ref</code> to be reactive.
Attempts:
2 left
💡 Hint
Think about Vue 3's Proxy-based reactivity and how it handles dynamic property additions.

Practice

(1/5)
1. What does the reactive function in Vue do?
easy
A. It converts a reactive object into a plain JavaScript object.
B. It makes an object reactive so Vue tracks its changes automatically.
C. It creates a new Vue component.
D. It registers a global event listener.

Solution

  1. Step 1: Understand the purpose of reactive

    The reactive function wraps an object so Vue can track its property changes.
  2. Step 2: Compare options with the purpose

    Only It makes an object reactive so Vue tracks its changes automatically. correctly describes this behavior; others describe unrelated Vue features.
  3. Final Answer:

    It makes an object reactive so Vue tracks its changes automatically. -> Option B
  4. Quick Check:

    reactive() tracks object changes = C [OK]
Hint: Remember: reactive() wraps objects to track changes [OK]
Common Mistakes:
  • Confusing reactive() with component creation
  • Thinking reactive() converts reactive objects back to plain objects
  • Assuming reactive() adds event listeners
2. Which of the following is the correct way to create a reactive object in Vue 3?
easy
A. const state = reactive.count = 0
B. const state = reactive = { count: 0 }
C. const state = reactive([count: 0])
D. const state = reactive({ count: 0 })

Solution

  1. Step 1: Check the syntax for reactive object creation

    The correct syntax is calling reactive() with an object: reactive({ count: 0 }).
  2. Step 2: Identify syntax errors in other options

    const state = reactive = { count: 0 } uses invalid assignment, C uses array syntax incorrectly, D tries to assign property directly without reactive call.
  3. Final Answer:

    const state = reactive({ count: 0 }) -> Option D
  4. Quick Check:

    reactive() called with object literal = A [OK]
Hint: Use reactive() with an object inside parentheses [OK]
Common Mistakes:
  • Using assignment instead of function call
  • Passing array instead of object
  • Assigning properties outside reactive()
3. Given this code, what will be logged to the console?
import { reactive } from 'vue'
const state = reactive({ count: 1 })
state.count = 5
console.log(state.count)
medium
A. 5
B. Error: Cannot assign to reactive property
C. undefined
D. 1

Solution

  1. Step 1: Understand reactive object property update

    Assigning state.count = 5 updates the reactive property value.
  2. Step 2: Check the logged value

    Logging state.count after update outputs the new value 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    Updated reactive property logs new value = D [OK]
Hint: Reactive objects update properties directly and reflect changes [OK]
Common Mistakes:
  • Thinking reactive objects are immutable
  • Expecting old value after update
  • Confusing reactive with ref syntax
4. What is wrong with this Vue 3 code snippet?
import { reactive } from 'vue'
const state = reactive({ count: 0 })
state = { count: 5 }
medium
A. You cannot reassign a reactive object variable directly.
B. The reactive function requires an array, not an object.
C. The property count must be declared with ref(), not reactive.
D. Reactive objects cannot have numeric properties.

Solution

  1. Step 1: Identify the error in variable reassignment

    The variable state is declared with const, so it cannot be reassigned.
  2. Step 2: Understand reactive object usage

    Reactive objects are mutable in their properties but the variable holding them cannot be reassigned if declared const.
  3. Final Answer:

    You cannot reassign a reactive object variable directly. -> Option A
  4. Quick Check:

    const variable reassignment causes error = A [OK]
Hint: const variables can't be reassigned, only their properties changed [OK]
Common Mistakes:
  • Trying to reassign const reactive variables
  • Confusing reactive() input types
  • Thinking reactive objects restrict property types
5. You want to create a reactive object to track a user's profile with nested address info. Which code correctly makes the entire object reactive so changes to nested properties update the UI?
hard
A. const profile = reactive({ name: 'Alice' }); profile.address = { city: 'NY', zip: '10001' }
B. const profile = { name: 'Alice', address: reactive({ city: 'NY', zip: '10001' }) }
C. const profile = reactive({ name: 'Alice', address: { city: 'NY', zip: '10001' } })
D. const profile = reactive({ name: 'Alice', address: ref({ city: 'NY', zip: '10001' }) })

Solution

  1. Step 1: Understand nested reactivity with reactive()

    Using reactive() on the whole object recursively makes nested properties reactive automatically.
  2. Step 2: Analyze each option's approach

    const profile = reactive({ name: 'Alice', address: { city: 'NY', zip: '10001' } }) wraps the entire object including nested address, so all changes are reactive. const profile = reactive({ name: 'Alice' }); profile.address = { city: 'NY', zip: '10001' } assigns a plain object to nested property after reactive call, losing reactivity for nested changes. const profile = { name: 'Alice', address: reactive({ city: 'NY', zip: '10001' }) } mixes reactive inside plain object, which breaks reactivity tracking. const profile = reactive({ name: 'Alice', address: ref({ city: 'NY', zip: '10001' }) }) mixes ref inside reactive, which is invalid since reactive expects plain objects.
  3. Final Answer:

    const profile = reactive({ name: 'Alice', address: { city: 'NY', zip: '10001' } }) -> Option C
  4. Quick Check:

    reactive() wraps whole nested object = B [OK]
Hint: Wrap entire nested object with reactive() for full reactivity [OK]
Common Mistakes:
  • Wrapping nested objects separately after reactive() call
  • Mixing reactive and plain objects incorrectly
  • Assigning plain objects to reactive properties
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