翻译练习 react-Rendering Elements

Rendering Elements
渲染组件

Elements are the smallest building blocks of React apps.
组件是react app 最简单的基石

An element describes what you want to see on the screen:
一个组件描述了你想要在屏幕上看到什么

const element = <h1>Hello, world</h1>;

Unlike browser DOM elements, React elements are plain objects, and are cheap to create. React DOM takes care of updating the DOM to match the React elements.
和浏览器dom元素不同的是,react 组件是普通的object并且非常容易创建,react dom会负责dom节点的更新与react 组件保持一致

Note:
One might confuse elements with a more widely known concept of “components”. We will introduce components in the next section. Elements are what components are “made of”, and we encourage you to read this section before jumping ahead.
注意:
你可能会把元素和各种已知组件概念弄混,我门将在下一章介绍组件,组件是由元素组成的,并且我们建议你跳到下一章之前先阅读此章节

Rendering an Element into the DOM
将一个元素渲染为dom

Let’s say there is a <div> somewhere in your HTML file:
现在在你的html文件中写一个div

<div id="root"></div>

We call this a “root” DOM node because everything inside it will be managed by React DOM.
我们称它是一个跟Dom节点,因为我们通过React dom管理的任何元素都会在这个里面

Applications built with just React usually have a single root DOM node. If you are integrating React into an existing app, you may have as many isolated root DOM nodes as you like.
react应用构建一般来说仅仅需要一个根Dom节点。如果你想整合react到已存在的app中,你可以根据你的需要创造多个独立的根节点

To render a React element into a root DOM node, pass both to ReactDOM.render():
想要在一个根节点内渲染一个react组件,通过调用ReactDOM。render()

const element = <h1>Hello, world</h1>;
ReactDOM.render(element, document.getElementById('root'));

Try it on CodePen
在codePen中尝试一下

It displays “Hello, world” on the page.
它建在页面中展示“hello world”

Updating the Rendered Element
更新渲染的元素

React elements are immutable. Once you create an element, you can’t change its children or attributes. An element is like a single frame in a movie: it represents the UI at a certain point in time.
react 元素是不可改变的,一旦你创造了一个元素,你不能改变它的子元素或者属性。一个原始就像电影里面的一幕。它展示当前时间点的ui

With our knowledge so far, the only way to update the UI is to create a new element, and pass it to ReactDOM.render().
根据目前我们所学的知识,创造新的一个元素是更新ui的唯一一种方式,并将它传入到ReactDOM。render中调用

Consider this ticking clock example
思考下面这个秒表的例子

function tick() {
  const element = (
    <div>
      <h1>Hello, world!</h1>
      <h2>It is {new Date().toLocaleTimeString()}.</h2>
    </div>
  );
  ReactDOM.render(element, document.getElementById('root'));
}

setInterval(tick, 1000);

Try it on CodePen
在CodePen中尝试

It calls ReactDOM.render() every second from a setInterval() callback.
它通过setInterval()的回调函数中每秒调用一次ReactDOM。render()

Note:
In practice, most React apps only call ReactDOM.render() once. In the next sections we will learn how such code gets encapsulated into stateful components.
We recommend that you don’t skip topics because they build on each other.
注意:
在实践中,react app 通常只会调用一次ReactDOM。render()一次,在下一个章节,我门将学习如何将这样的代码封装到状态化的组件中去
我门 推荐 你不要跳跃的去阅读,因为它们都是互相紧密联系的

React Only Updates What’s Necessary
react 仅仅更新需要的东西

React DOM compares the element and its children to the previous one, and only applies the DOM updates necessary to bring the DOM to the desired state.
React dom 会比较当前元素和它的子元素和之前的区别,并且仅仅只会更新必要的dom去达到想要的状态

You can verify by inspecting the last example with the browser tools:
你可以通过浏览器工具检查最后一个例子去证实这个说法

Even though we create an element describing the whole UI tree on every tick, only the text node whose contents has changed gets updated by React DOM.
尽管我们在每一秒创建了一个元素描述整个ui树,但是React DOM 的更新 仅仅改变了文本节点的内容

In our experience, thinking about how the UI should look at any given moment rather than how to change it over time eliminates a whole class of bugs.
根据我们以往的经验,考虑ui在任意时刻的状态,而不是随着时间的改变而改变,会减少许多bug

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容

  • 原教程内容详见精益 React 学习指南,这只是我在学习过程中的一些阅读笔记,个人觉得该教程讲解深入浅出,比目前大...
    leonaxiong阅读 2,770评论 1 18
  • Yes, React is taking over front-end development. The ques...
    Transnet2014阅读 927评论 0 2
  • 3. JSX JSX是对JavaScript语言的一个扩展语法, 用于生产React“元素”,建议在描述UI的时候...
    pixels阅读 2,758评论 0 24
  • —— 基础知识、JSX介绍、React 元素、组件和属性、状态和生命周期 此文档来自 React 官方文档,在英文...
    thelastcookies阅读 461评论 0 1
  • 关于JSX 考虑这样一段代码:const element = Hello, world! ;这段代码既不是字符串...
    带三本书阅读 348评论 0 1