- JSX produces React "elements".
- JSX用于产生React元素。
在JSX语法中,遇到HTML标签(以<开头)就用HTML规则解析,遇到代码块(以{开头)就用JavaScript规则解析。React 可以渲染 HTML 标签 (strings) 或 React 组件 (classes)。
要渲染 HTML 标签,只需在 JSX 里使用小写字母开头的标签名。
要渲染 React 模块,只需创建一个大写字母开头的本地变量。
MyComponentcreateClass({/*...*/});
const user = { firstName: 'Oscar',lastName: 'Liu' };这段代码是定义一个对象变量。
const element = ( <h1> Hello,{formatName(user)}! </h1> );
After compilation,JSX expressions become regular JavaScript objects.
This means that you can use JSX inside ofif
statements andfor
loops,assign it to variables,accept it as arguments,and return it from functions:
@H_404_13@ You may also use curly braces to embed a JavaScript expression in an attribute
@H_404_13@ 用花括号括起表达式作为属性的值,这种做法就是用JSX指定属性。 @H_404_13@ const element =<imgsrc={user.avatarUrl}></img>;
@H_404_13@
@H_404_13@ You should either use quotes (for string values) or curly braces (for expressions),but not both in the same attribute.
@H_404_13@ 不要同时在一个属性里面使用“”和花括号,就是不要混用。 @H_404_13@
@H_404_13@
@H_404_13@
- 用JSX指定Children
- If a tag is empty,you may close it immediately with
/>
- 如果一个tag是空,你可以用/>立刻close它。
- JSX tags may contain children
- JSX标签有可能包含Children
const element = ( <div> <h1>Hello!</h1> <h2>Good to see you here.</h2> </div> );
Since JSX is closer to JavaScript than HTML,React DOM uses
camelCase
property naming convention instead of HTML attribute names.For example,
class
becomesclassName
in JSX,andtabindex
becomestabIndex.
@H_404_13@ @H_404_13@ React DOM中像html属性名字我们用驼峰原则来命名。