Skip to content

Move examples of rendering elements from CodePen into ./examples #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 5 additions & 26 deletions content/docs/rendering-elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,9 @@ Applications built with just React usually have a single root DOM node. If you a

To render a React element into a root DOM node, pass both to `ReactDOM.render()`:

```js
const element = <h1>Hello, world</h1>;
ReactDOM.render(
element,
document.getElementById('root')
);
```
`embed:rendering-elements/render-an-element.js`

[Try it on CodePen.](http://codepen.io/gaearon/pen/rrpgNB?editors=1010)
[](codepen://rendering-elements/render-an-element)

It displays "Hello, world" on the page.

Expand All @@ -55,24 +49,9 @@ With our knowledge so far, the only way to update the UI is to create a new elem

Consider this ticking clock example:

```js{8-11}
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);
```
`embed:rendering-elements/update-rendered-element.js`

[Try it on CodePen.](http://codepen.io/gaearon/pen/gwoJZk?editors=0010)
[](codepen://rendering-elements/update-rendered-element)

It calls `ReactDOM.render()` every second from a [`setInterval()`](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval) callback.

Expand All @@ -86,7 +65,7 @@ It calls `ReactDOM.render()` every second from a [`setInterval()`](https://devel

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.

You can verify by inspecting the [last example](http://codepen.io/gaearon/pen/gwoJZk?editors=0010) with the browser tools:
You can verify by inspecting the [last example](codepen://rendering-elements/update-rendered-element) with the browser tools:

![DOM inspector showing granular updates](../images/docs/granular-dom-updates.gif)

Expand Down
5 changes: 5 additions & 0 deletions examples/rendering-elements/render-an-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const element = <h1>Hello, world</h1>;
ReactDOM.render(
element,
document.getElementById('root')
);
18 changes: 18 additions & 0 deletions examples/rendering-elements/update-rendered-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function tick() {
const element = (
<div>
<h1>Hello, world!</h1>
<h2>
It is{' '}
{new Date().toLocaleTimeString()}.
</h2>
</div>
);
// highlight-range{1-4}
ReactDOM.render(
element,
document.getElementById('root')
);
}

setInterval(tick, 1000);