1. What is JSX and how does it work under the hood?
❓ Context: Explain JSX syntax, how it compiles to JavaScript, and why React 17+ no longer requires importing React in JSX files.
JSX (JavaScript XML) is a syntax extension for JavaScript that allows writing HTML-like markup inside JavaScript files.
How It Works:
Rules of JSX:
// JSX syntax in your code
function Profile({ user }) {
return (
<div className="card">
<img src={user.avatar} alt={user.name} />
<h2>{user.name}</h2>
</div>
);
}
// What React 17+ compiler transforms it to (using react/jsx-runtime):
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
function Profile({ user }) {
return _jsxs("div", {
className: "card",
children: [
_jsx("img", { src: user.avatar, alt: user.name }),
_jsx("h2", { children: user.name })
]
});
}JSX is syntactic sugar for React element creation. React 17+ uses react/jsx-runtime so importing React is optional for JSX.