JSX
一句话解释
JSX 是语法 extension for JavaScript 那个 lets 你编写 HTML-如 markup directly 内部你的 JavaScript code.
用大白话说
JSX (JavaScript XML) 是语法 extension 那个制作编写 React 组件 feel 如编写 HTML. 相反的 calling React.createElement() dozens 的次数, 你编写 familiar 标签如 <div>, <h1>, and <button> right inside your JavaScript.
Under the hood, JSX is compiled to React.createElement() calls by tools like Babel or the TypeScript compiler. A line like <h1>Hello</h1> becomes React.createElement('h1', 空, 'Hello'). You never write this by hand — JSX is purely a developer convenience.
JSX is not valid JavaScript on its own; it must be compiled. Inside JSX you can embed any JavaScript expression by wrapping it in curly braces {}, which makes it powerful for dynamic content.
示例
const name = "Ada";
const element = <h1>Hello, {name}!</h1>;
// The above compiles to:
// const element = React.createElement("h1", null, "Hello, ", name, "!"); curly braces embed JavaScript 变量 name 到 JSX markup. Babel transforms JSX 到 createElement call.
常见混淆
容易混淆的对象: html
关键区别: JSX looks 如 HTML 但它 是 JavaScript. 标签必须是 关闭 (self-关闭 or paired),
类becomesclassName, andforbecomeshtmlFor因为这些是 reserved words in JS.
相关术语
相关资源
相关课程
- JSX 基础
学习 JSX 语法, 表达式, and compilation.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。