JSX 基础
JSX 语法规则, embedding 表达式, and 如何 JSX 编译到 React.createElement calls.
你将学到
- 什么 JSX 是 and 为什么它 looks 如 HTML 但是 JavaScript
- 如何 embed JavaScript 表达式内部 JSX 使用 curly braces
- 规则的 JSX: 单个根, self-关闭标签, className 对比类
- 如何 JSX 编译下 到 React.createElement
概念讲解
What Is JSX?
JSX 是一种syntax extension for JavaScript that lets you write markup that looks like HTML directly inside your JavaScript code。 It is not valid JavaScript by itself — a compiler like Babel or the TypeScript compiler transforms it into regular JavaScript function calls. Despite looking like HTML, JSX is fundamentally JavaScript, which means you can use the full power of the language inside it.
Embedding Expressions
You embed any valid JavaScript expression inside JSX using curly braces {}. This includes variables, function calls, arithmetic, and ternary operators. You cannot embed statements like 如果 or for directly — use ternary operators or extract logic into variables or helper functions above the return statement.
JSX Rules
JSX has a few strict rules. First, a component must return a single root element — wrap siblings in a <div>, a Fragment <></>, or an array. Second, all tags must be closed — <img /> not <img>. Third, use className instead of 类 for CSS classes because 类 is a reserved word in JavaScript. Fourth, style attributes receive an object, not a string: 样式={{ 颜色: "red" }}.
How JSX Compiles
Every JSX element is compiled to a React.createElement() call. Writing <h1 className="greeting">Hello</h1> becomes React.createElement("h1", { className: "greeting" }, "Hello"). Understanding this helps you debug errors and appreciate that JSX is just syntactic sugar — there is no magic, only function calls that return objects describing the UI.
JSX vs HTML Differences
While JSX looks like HTML, there are important differences. Attributes use camelCase (onClick not onclick, tabIndex not tabindex). Style is an object (样式={{ 颜色: "red" }}) not a string. Boolean attributes like 禁用 need an explicit value. Comments use {/* comment */} syntax inside JSX. These differences exist because JSX is JavaScript, and the attribute names follow JavaScript conventions.
示例代码
// Embedding expressions in JSX with curly braces.
function Profile({ user, isLoggedIn }) {
const fullName = `${user.first} ${user.last}`;
const greeting = isLoggedIn ? "Welcome back" : "Please log in";
return (
<div className="profile-card">
<h2>{greeting}, {fullName}!</h2>
<p>Member since {user.joinedYear} ({new Date().getFullYear() - user.joinedYear} years)</p>
<img src={user.avatarUrl} alt={fullName} className="avatar" />
</div>
);
}
Notice className 相反的 类, self-关闭 img 标签, and curly braces wrapping 每个 JavaScript 表达式包含 template literals and arithmetic.
动手试一试
- HTML Entity Encoder
JSX escapes 特殊 characters — 理解 HTML entity encoding.
- Code Minifier
See 如何 JSX 获取 minified in production 构建.
常见错误
错误写法
Using 类相反的 className for CSS 类
正确做法
类是 reserved word in JavaScript. In JSX, 总是 use className 到 apply CSS 类. Use 样式={{}} for inline 样式.
错误写法
Returning 多个 sibling 元素不使用 wrapper
正确做法
Wrap 它们 in 单个 parent <div>, React 片段 <></>, or 返回数组使用键 Props. 组件必须返回 one 根元素.
练习 JSX 基础
5 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。