JSX lets you write HTML-like code inside JavaScript. In Vue, it helps you build components using JavaScript syntax instead of templates.
JSX in Vue components
import { defineComponent } from 'vue'; export default defineComponent({ setup() { return () => ( <div>Hello JSX in Vue!</div> ); } });
JSX code goes inside the return () => ( ... ) function in setup().
Use parentheses ( ) to wrap JSX elements for clarity.
export default { setup() { return () => <h1>Title with JSX</h1>; } };
export default { setup() { const name = 'Vue'; return () => <p>Hello, {name}!</p>; } };
map() with keys for each item.export default { setup() { const items = ['Apple', 'Banana', 'Cherry']; return () => ( <ul> {items.map(item => <li key={item}>{item}</li>)} </ul> ); } };
This Vue component uses JSX to show a count and a button. Clicking the button increases the count. It uses ref for reactive state and onClick for the button event.
The button has an aria-label for accessibility.
import { defineComponent, ref } from 'vue'; export default defineComponent({ setup() { const count = ref(0); const increment = () => { count.value++; }; return () => ( <div> <p>Count: {count.value}</p> <button onClick={increment} aria-label="Increment count">Add 1</button> </div> ); } });
JSX in Vue requires a build step with tools like Vite or Vue CLI configured to support JSX.
Use onClick (camelCase) for event handlers in JSX, not lowercase onclick.
Always add key props when rendering lists to help Vue track elements efficiently.
JSX lets you write HTML-like code inside JavaScript functions in Vue components.
It is useful for dynamic UI and when you prefer JavaScript over templates.
Remember to use return () => ( ... ) in setup() and wrap JSX in parentheses.
Practice
Solution
Step 1: Understand JSX role in Vue
JSX allows writing HTML-like syntax inside JavaScript, making UI code dynamic and easier to manage.Step 2: Compare options
Only To write HTML-like code inside JavaScript functions for UI rendering correctly describes JSX's purpose. Others confuse JSX with CSS or removing JavaScript.Final Answer:
To write HTML-like code inside JavaScript functions for UI rendering -> Option AQuick Check:
JSX = HTML-like code in JS [OK]
- Thinking JSX replaces JavaScript entirely
- Confusing JSX with CSS styling
- Believing JSX removes the need for Vue components
setup() function in Vue 3?Solution
Step 1: Recall Vue 3 setup JSX syntax
In Vue 3,setup()returns a render function, usually an arrow function returning JSX wrapped in parentheses.Step 2: Evaluate options
return () => (Hello); correctly returns an arrow function with JSX. returnHello; returns JSX directly (invalid). return function() {Hello}; returns a function but misses return statement. return { template: 'Hello' }; uses template string, not JSX.Final Answer:
return () => (<div>Hello</div>); -> Option CQuick Check:
setup returns arrow function with JSX [OK]
- Returning JSX directly without a function
- Using function without return statement
- Confusing JSX with template strings
import { defineComponent } from 'vue';
export default defineComponent({
setup() {
const name = 'Vue';
return () => (
Hello, {name}!
);
}
});Solution
Step 1: Understand JSX expression interpolation
In JSX, curly braces insert JavaScript expressions. Here, {name} inserts the string 'Vue'.Step 2: Predict rendered HTML
The component renders a div with an h1 containing 'Hello, Vue!'. No syntax errors occur.Final Answer:
<div><h1>Hello, Vue!</h1></div> -> Option AQuick Check:
JSX {name} inserts variable value [OK]
- Thinking JSX shows variable name literally
- Expecting empty output if variable used
- Assuming JSX causes syntax errors without Babel
import { defineComponent } from 'vue';
export default defineComponent({
setup() {
const count = 0;
return () => {
{count}
};
}
});Solution
Step 1: Check JSX return syntax in arrow function
The arrow function uses curly braces but does not return JSX explicitly or wrap JSX in parentheses, causing no output.Step 2: Correct syntax for returning JSX
JSX must be wrapped in parentheses and returned explicitly, e.g.,() => ( <button>{count}</button> ).Final Answer:
Missing parentheses around JSX in return function -> Option DQuick Check:
JSX must be wrapped in parentheses and returned [OK]
- Forgetting to return JSX explicitly
- Not wrapping JSX in parentheses
- Thinking reactive is required for static display
setup()?Solution
Step 1: Understand how to render lists in JSX
JSX uses JavaScript expressions.map()returns an array of JSX elements, perfect for rendering lists.Step 2: Evaluate each option
return () => (- {props.items.map(item =>
- {item} )}
map()to return- elements. return () => (
- {props.items.forEach(item =>
- {item} )}
forEachwhich returns undefined. return () => (- {for (const item of props.items)
- {item} }
- {props.items.map(item => '
- ' + item + ' ')}
Final Answer:
return () => (<ul>{props.items.map(item => <li>{item}</li>)}); -> Option BQuick Check:
Use map() to return JSX list items [OK]
- Using forEach instead of map for JSX lists
- Trying to use for loops inside JSX expressions
- Returning strings instead of JSX elements
Ready to prove it?
Pass the test and this pattern moves unknown
