资讯专栏INFORMATION COLUMN

【译】介绍JSX

ymyang / 1294人阅读

摘要:介绍我们来看一下下面的变量声明这是有意思的标记语法既不是字符串又不是。也是一个表达式编译后表达式成为常规的对象。防止注入攻击中直接嵌套用户在表单表单中输入的值是安全的。这有助于防止攻击跨站脚本。读取这些对象并使用它们构造并保持更新。

下面是react官方文档的个人翻译,如有翻译错误,请多多指出
原文地址:https://facebook.github.io/re...
特别感谢Hevaen,同时也向豪大React群所有成员表示感谢,从你们身上我学到很多。

介绍JSX

我们来看一下下面的变量声明

const element = 

Hello, world!

;

这是有意思的标记语法既不是字符串又不是HTML。

It is called JSX, and it is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript.

我们称他为JSX。它是属于JavaScript的语法拓展。我们希望react用jsx去描述UI应该是什么样子的。JSX也许会让你想到某些模板语言,但它带有JavaScript的全部威力。

JSX produces React "elements". We will explore rendering them to the DOM in the next section. Below, you can find the basics of JSX necessary to get you started.

JSX 生成React的“元素”。我们将会在下一章探索他们是怎么在DOM里渲染的。接下来,你能找到JSX最重要的基础知识来让你开始学习

Embedding Expressions in JSX 在JSX里面插入表达式

You can embed any JavaScript expression in JSX by wrapping it in curly braces.
你能用花括号包住JavaScript 表达式然后插入JSX里

For example, 2 + 2, user.firstName, and formatName(user) are all valid expressions:
例如,2 + 2, user.firstName,和 formatName(user)都是合法的表达式。

function formatName(user) {
  return user.firstName + " " + user.lastName;
}

const user = {
  firstName: "Harper",
  lastName: "Perez"
};

const element = (
  

Hello, {formatName(user)}!

); ReactDOM.render( element, document.getElementById("root") );

We split JSX over multiple lines for readability. While it isn"t required, when doing this, we also recommend wrapping it in parentheses to avoid the pitfalls of automatic semicolon insertion.

为了可读性,我们把JSX分到多个行里。虽然不是必需的,当这样做时,我们还建议包在大括号来避免自动分号的陷阱。

JSX is an Expression Too

JSX也是一个表达式
After compilation, JSX expressions become regular JavaScript objects.
编译后,JSX表达式成为常规的JavaScript对象。
This means that you can use JSX inside of if statements and for loops, assign it to variables, accept it as arguments, and return it from functions:
这意味着您可以在JSX中使用 if语句和循环,将其分配给变量,接受它作为参数,并从函数中返回。

Specifying Attributes with JSX

You may use quotes to specify string literals as attributes:

你可以使用引号指定字符串的属性:

const element = 
;

You may also use curly braces to embed a JavaScript expression in an attribute:
你也可以使用括号将JavaScript表达式嵌入到一个属性:

const element = , like XML:
如果一个标签是空的,你可以立即关闭它/ >,如XML:

const element = ;

JSX tags may contain children:
JSX标签可以包含子元素:

const element = (
  

Hello!

Good to see you here.

);

Caveat:
Since JSX is closer to JavaScript than HTML, React DOM uses camelCase property naming convention instead of HTML attribute names.
For example, class becomes className in JSX, and tabindex becomes tabIndex.
警告:自从JSX更接近JavaScript而不是HTML, React DOM中我们使用class作为标签的属性,而在JSX中我们使用className(因为class是js的保留字)
例如,类成为JSX className,tabindex变成了tabindex

JSX Prevents Injection Attacks

JSX防止注入攻击

It is safe to embed user input in JSX:

JSX中直接嵌套用户在表单表单中输入的值是安全的。

const title = response.potentiallyMaliciousInput;
// This is safe:
const element = 

{title}

;

By default, React DOM escapes any values embedded in JSX before rendering them.
默认情况下,React DOM会在渲染元素前转义JSX中的任何嵌套的值
Thus it ensures that you can never inject anything that"s not explicitly written in your application.
因此,确保你永远不能注入任何没有明确的写在您的应用程序
Everything is converted to a string before being rendered.
一切都是在渲染之前转换为一个字符串。
This helps prevent XSS (cross-site-scripting) attacks.
这有助于防止XSS攻击(跨站脚本)。

JSX Represents Objects

JSX对象

Babel compiles JSX down to React.createElement() calls.
Babel 编译 JSX 成 React.createElement() 调用。

These two examples are identical:
这两个例子都是相同的:

const element = (
  

Hello, world!

);
const element = React.createElement(
  "h1",
  {className: "greeting"},
  "Hello, world!"
);

React.createElement() performs a few checks to help you write bug-free code but essentially it creates an object like this:

React.createElement()执行几个检查,帮助你写出没有错误代码但本质上它创建一个对象是这样的:

// Note: this structure is simplified
const element = {
  type: "h1",
  props: {
    className: "greeting",
    children: "Hello, world"
  }
};

These objects are called "React elements". You can think of them as descriptions of what you want to see on the screen.React reads these objects and uses them to construct the DOM and keep it up to date.We will explore rendering React elements to the DOM in the next section.

这些对象被称为“React元素”。你可以把它们作为描述你想在屏幕上看到的东西。
React读取这些对象,并使用它们构造DOM并保持更新。在下一节,我们将探讨渲染React元素到DOM上。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/79989.html

相关文章

  • 】一个小时搭建一个全栈Web应用框架(上)

    摘要:初始项目设置我们将使用包管理器来处理依赖项。使用包管理器可以使您的项目依赖项保持最新状态,并能够获取和安装最新的包。是小型应用的最佳选择之一。 翻译:疯狂的技术宅英文标题:Creating a full-stack web application with Python, NPM, Webpack and React英文原文:https://codeburst.io/creating....

    wizChen 评论0 收藏0
  • 】一个小时搭建一个全栈Web应用框架(上)

    摘要:初始项目设置我们将使用包管理器来处理依赖项。使用包管理器可以使您的项目依赖项保持最新状态,并能够获取和安装最新的包。是小型应用的最佳选择之一。 翻译:疯狂的技术宅英文标题:Creating a full-stack web application with Python, NPM, Webpack and React英文原文:https://codeburst.io/creating....

    Doyle 评论0 收藏0
  • [] 逐渐去掌握 React(作为一名 Angular 开发者)

    摘要:你是一个对感兴趣的开发者吗不用担心,这真的不会让你成为一个背叛者或其他什么,真的。事实上,这个想法并不是或独创的它只是一种很棒的软件开发实践方式。把代码分离到不同的文件里并不会自动导致关注点分离。 原文链接 : Getting to Grips with React (as an Angular developer)原文作者 : DAVE CEDDIA译者 : 李林璞(web前端领域)...

    channg 评论0 收藏0
  • []JSX:硬币的另一面

    摘要:它不过是硬币的另一面。因此,既然我们能够接受与通过这种方式混合在一块儿,那么是时候让介入并向我们展示硬币的另一面了第三阶段的并不是一个激进的改变,是因为我们这个行业从一开始就注定和应该是在一起的。 React框架刚刚发布的时候,JSX颠覆了很多人的想法。习惯了HTML标签与JavaScript代码分离的前端工程师们,看到JSX大概都会不禁吐槽:这些奇怪的标签出现在JavaScript里...

    mudiyouyou 评论0 收藏0
  • [] A Prettier JavaScript Formatter

    摘要:原文今天我发布一个格式化工具它的灵感来源于它对于和的语言特性有着高级的支持通过将解析为并且基于美化和打印会丢掉几乎全部的原始的代码风格从而保证代码风格的一致性跟不一样的在于它没有大量的和需要管理不过同时有一点也很重要一切都是确定好的我很高 原文 http://jlongster.com/A-Pretti... 今天我发布 Prettier, 一个 JavaScript 格式化工具. 它...

    elina 评论0 收藏0

发表评论

0条评论

ymyang

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<