0
0
Vueframework~20 mins

JSX in Vue components - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSX Vue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Vue JSX component render?
Consider this Vue 3 component using JSX syntax. What will it render in the browser?
Vue
import { defineComponent } from 'vue';

export default defineComponent({
  setup() {
    const message = 'Hello from JSX!';
    return () => <div><h1>{message}</h1><p>{message.length}</p></div>;
  }
});
A<div><h1>Hello from JSX!</h1><p>15</p></div>
B<div><h1>{message}</h1><p>{message.length}</p></div>
C<div><h1>Hello from JSX!</h1><p>Hello from JSX!</p></div>
D<div><h1>Hello from JSX!</h1><p>16</p></div>
Attempts:
2 left
💡 Hint
Remember JSX expressions inside curly braces evaluate JavaScript expressions.
📝 Syntax
intermediate
2:00remaining
Which JSX syntax is correct for binding a click event in Vue?
In Vue JSX, how do you correctly bind a click event handler named 'handleClick' to a button element?
A<button onClick={handleClick}>Click me</button>
B<button v-on:click={handleClick}>Click me</button>
C<button onClick:click={handleClick}>Click me</button>
D<button onClick={() => handleClick()}>Click me</button>
Attempts:
2 left
💡 Hint
Vue JSX uses camelCase event names similar to React.
🔧 Debug
advanced
2:00remaining
Why does this Vue JSX component throw an error?
This Vue component using JSX throws an error when rendering. What is the cause?
Vue
import { defineComponent, ref } from 'vue';

export default defineComponent({
  setup() {
    const count = ref(0);
    return () => <button onClick={count++}>Count: {count.value}</button>;
  }
});
AUsing 'count++' directly in onClick causes a runtime error because count is a ref object, not a number.
BThe JSX syntax is invalid because onClick must be lowercase 'onclick'.
CThe component is missing a return statement inside setup.
DThe count.value should be updated using count.value++ inside the onClick handler function.
Attempts:
2 left
💡 Hint
Remember that Vue refs are objects and you must update their .value property.
lifecycle
advanced
2:00remaining
How to use Vue lifecycle hooks inside a JSX component?
Which is the correct way to run code when a Vue JSX component mounts?
Vue
import { defineComponent, onMounted } from 'vue';

export default defineComponent({
  setup() {
    onMounted(() => {
      console.log('Mounted!');
    });
    return () => <div>Check console</div>;
  }
});
AUse a lifecycle method named componentDidMount inside the render function.
BUse the onMounted hook inside setup to run code after the component mounts, as shown.
CAdd a mounted() method in the component options object alongside setup.
DCall console.log directly inside the render function to simulate mounting.
Attempts:
2 left
💡 Hint
Vue 3 uses composition API lifecycle hooks inside setup.
state_output
expert
2:00remaining
What is the output after clicking the button twice?
Given this Vue JSX component, what will be the displayed count after clicking the button two times?
Vue
import { defineComponent, ref } from 'vue';

export default defineComponent({
  setup() {
    const count = ref(0);
    const increment = () => {
      count.value += 1;
      count.value += 1;
    };
    return () => <button onClick={increment}>Count: {count.value}</button>;
  }
});
ACount: 1
BCount: 2
CCount: 4
DCount: 0
Attempts:
2 left
💡 Hint
Each click runs increment which adds 2 to count.value.