0
0
Vueframework~20 mins

Setup function basics in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Setup Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Vue 3 setup function render?
Consider this Vue 3 component using the setup function. What will be displayed on the page?
Vue
import { ref } from 'vue';

export default {
  setup() {
    const message = ref('Hello Vue 3!');
    return { message };
  },
  template: `<p>{{ message }}</p>`
};
A<p>Hello Vue 3!</p>
B<p>[object Object]</p>
C<p></p>
D<p>message</p>
Attempts:
2 left
💡 Hint
Remember that ref creates a reactive object and you can use it directly in the template.
📝 Syntax
intermediate
2:00remaining
Which setup function code is syntactically correct?
Identify the option that correctly defines a setup function returning a reactive count variable.
A
setup() {
  const count = ref(0);
  return count;
}
B
setup() {
  const count = ref(0);
  return { count: count.value };
}
C
setup() {
  const count = ref(0);
  return count.value;
}
D
setup() {
  const count = ref(0);
  return { count };
}
Attempts:
2 left
💡 Hint
The setup function must return an object with reactive properties for the template to access.
state_output
advanced
2:00remaining
What is the value of 'count' after clicking the button twice?
Given this Vue 3 component, what will be the displayed count after two button clicks?
Vue
import { ref } from 'vue';

export default {
  setup() {
    const count = ref(0);
    function increment() {
      count.value++;
    }
    return { count, increment };
  },
  template: `<div><button @click="increment">Add</button><p>{{ count }}</p></div>`
};
A1
B2
C0
DNaN
Attempts:
2 left
💡 Hint
Each click increases count by 1 starting from 0.
🔧 Debug
advanced
2:00remaining
Why does this setup function cause a runtime error?
Examine the setup function below. Why will this code cause an error when the component renders?
Vue
import { ref } from 'vue';

export default {
  setup() {
    const name = ref('Alice');
    return name;
  },
  template: `<p>{{ name }}</p>`
};
AThe setup function returns a ref directly, but the template expects an object with properties.
BThe ref 'name' is not initialized properly with a string value.
CThe template syntax is invalid and cannot access 'name'.
DThe setup function is missing a return statement.
Attempts:
2 left
💡 Hint
Vue expects setup to return an object with keys for template bindings.
🧠 Conceptual
expert
2:00remaining
Which statement about the Vue 3 setup function is true?
Select the correct statement about the setup function in Vue 3 components.
AThe setup function is optional and cannot use Vue's reactivity system.
BThe setup function runs after the component is mounted and cannot access props.
CThe setup function runs before the component is created and can only return reactive references or objects.
DThe setup function replaces the template and must return a render function.
Attempts:
2 left
💡 Hint
Think about when setup runs and what it returns for the component.