0
0
Vueframework~10 mins

JSX in Vue components - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - JSX in Vue components
Define Vue component
Write render function using JSX
Vue compiles JSX to render function
Render function returns VNodes
Vue renders VNodes to DOM
User interacts -> state changes
Component re-renders with updated JSX output
JSX in Vue components works by writing a render function with JSX syntax, which Vue compiles to virtual nodes that update the DOM reactively.
Execution Sample
Vue
import { defineComponent, ref } from 'vue';

export default defineComponent({
  setup() {
    const count = ref(0);
    return () => <button onClick={() => count.value++}>Count: {count.value}</button>;
  }
});
A Vue component using JSX to render a button that increments a count on click.
Execution Table
StepActionJSX EvaluationState BeforeState AfterRendered Output
1Component setup runsJSX function createdcount=undefinedcount=0<button>Count: 0</button>
2Initial renderRender function calledcount=0count=0<button>Count: 0</button>
3User clicks buttononClick handler runscount=0count=1<button>Count: 1</button>
4Re-render triggeredRender function calledcount=1count=1<button>Count: 1</button>
5User clicks button againonClick handler runscount=1count=2<button>Count: 2</button>
6Re-render triggeredRender function calledcount=2count=2<button>Count: 2</button>
7No more clicksNo actioncount=2count=2<button>Count: 2</button>
💡 User stops clicking, component state stabilizes with count=2
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
count.valueundefined0122
Key Moments - 3 Insights
Why does the button text update when count changes?
Because the render function returns JSX using count.value, and Vue tracks count as reactive. When count.value changes (see steps 3 and 5), Vue re-runs the render function to update the DOM (steps 4 and 6).
Is JSX a template or a function in Vue?
JSX in Vue is used inside a render function (see step 1). It is not a separate template but JavaScript code that returns virtual nodes.
What happens if you forget to use count.value inside JSX?
The displayed count won't update because Vue tracks reactivity on count.value. Using count directly won't trigger updates (not shown in table but important).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the rendered output after step 3?
A<button>Count: 0</button>
B<button>Count: 1</button>
C<button>Count: 2</button>
D<button>Count: undefined</button>
💡 Hint
Check the 'Rendered Output' column at step 3 in the execution_table.
At which step does the component first re-render after a state change?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look for 'Re-render triggered' in the 'Action' column of execution_table.
If count.value was not reactive, what would happen to the rendered output after clicks?
AOutput never updates after initial render
BComponent crashes
COutput updates normally
DOutput updates but with delay
💡 Hint
Refer to variable_tracker and how Vue reactivity triggers re-renders.
Concept Snapshot
JSX in Vue components:
- Write JSX inside a render function returned from setup()
- Use reactive refs (e.g., count.value) inside JSX
- Vue compiles JSX to virtual DOM nodes
- State changes trigger re-render with updated JSX output
- JSX allows JavaScript logic inside Vue rendering
Full Transcript
This visual trace shows how JSX works inside a Vue component. The component defines a reactive count variable using ref. The render function returns JSX that displays the count and increments it on button click. Each click updates count.value, triggering Vue to re-run the render function and update the DOM with new JSX output. The execution table tracks each step: setup, initial render, clicks, and re-renders. The variable tracker shows count.value changing from 0 to 2. Key moments clarify why JSX updates with reactive state and how JSX is a render function, not a template. The quiz tests understanding of rendered output and reactivity. This helps beginners see how JSX and Vue reactivity work together to update the UI.