# Dynamic Pages

In the beginning, people created HTML pages by typing them in (just as we have been doing). They quickly realized that a lot of pages share a lot of content: navigation menus, contact info, and so on. The nearly universal response was to create a template and embed commands to include other snippets of HTML (like headers) and loop over data structures to create lists and tables. This is called server-side page generation: the HTML is generated on the server, and it was popular because that’s where the data was, and that was the only place complex code could be run. (This tutorial uses a templating tool called Jekyll. It’s clumsy and limited, but it’s the default on GitHub.)

Server-side generation can be done statically or dynamically, i.e., pages can be compiled once, stored on disk, and served thereafter, or each page can be recompiled whenever it’s needed, which makes it easy to include dynamic elements like today’s top news story (f:dynamic-alternatives).

As browsers and JavaScript became more powerful, the balance shifted toward client-side page generation. In this model, the browser fetches data from one or more servers and feeds that data to a JavaScript library that generates HTML in the browser for display. This allows the client to decide how best to render data, which is increasingly important as phones and tablets take over from desktop and laptop computers. It also moves the computational burden off the server and onto the client device, which lowers the cost of providing data.

Many, many JavaScript frameworks for client-side page generation have been created, and more are probably being developed right now. We have chosen React because it is freely available, widely used, well documented, simpler than many alternatives, and has a cool logo. Its central design principles are:

1. Page creators use functions to describe the HTML they want.
2. They then let React decide which functions to run when data changes.

We will show how to use it in pure JavaScript, then introduce a tool called JSX that simplifies things.

## Hello, World

Let’s begin by saying hello:

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>Hello React</title>
<script src="https://fb.me/react-15.0.1.js"></script>
<script src="https://fb.me/react-dom-15.0.1.js"></script>
<body>
<div id="app">
<!-- this is filled in -->
</div>
<script>
ReactDOM.render(
React.DOM.h1(null, "Hello React"),
document.getElementById("app")
)
</script>
</body>
</html>


The head of the page loads two React libraries from the web; we will use locally-installed libraries later. The body contains a div with an ID to make it findable. When our script runs, React will put the HTML it generates into this div.

The script itself create an h1 with the text “Hello, React” using React.DOM.h1, then finds the document element whose ID is "app" and uses ReactDOM.render to insert the former into the latter. This alters the representation of the page in memory, not the source of the page on disk; if we want to inspect the HTML, we have to do so in the browser. Finally, we put the script at the bottom of the page so that the browser will have turned the HTML into a DOM tree in memory before the script runs. We will come back and fix this later.

Inspection Tools

If you are using the Firefox browser, you can open the developer tools pane by going to the main menu and selecting Tools... Web Developer... Toggle Tools. A tabbed display will open in the bottom of your page; choose Inspector to view the content of your page and page’s CSS. As you move your mouse around the page itself, corresponding structural elements will be highlighted. It’s actually pretty cool…

The first parameter to React.DOM.h1 is null in the example above, but it can more generally be an object that specifies the attributes we want the newly-created node to have. There are a few quirks in this—for example, we have to use fontStyle rather than font-style so that the attribute object’s keys look like legal JavaScript variables—but the mechanism is surprisingly easy to use:

  <body>
<div id="app"></div>
<script>
const attributes = {
'style': {
'background': 'pink',
'fontStyle': 'italic'
}
}
ReactDOM.render(
React.DOM.h1(attributes, "Hello Stylish"),
document.getElementById("app")
)
</script>
</body>


## JSX

Writing nested functions is a clumsy way to write HTML, so most React programmers use a tool called JSX that translates HTML into JavaScript function calls. And yes, those JavaScript function calls then produce HTML—it’s a funny world. Here’s an example:

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>Hello JSX</title>
<script src="https://fb.me/react-15.0.1.js"></script>
<script src="https://fb.me/react-dom-15.0.1.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.js"></script>
<body>
<div id="app"></div>
<script type="text/babel">
ReactDOM.render(
<h1>Hello JSX</h1>,
document.getElementById('app')
)
</script>
</body>
</html>


Along with the two React libraries, this page includes a tool called Babel to translate a mixed of HTML and JavaScript into pure JavaScript. To trigger translation, we add the attribute type="text/babel" to the script tag.

Why bother? Because as the example above shows, it allows us to write <h1>Hello JSX</h1> instead of calling a function. More generally, JSX lets us put JavaScript inside our HTML (inside our JavaScript (inside our HTML)), so we can (for example) use map to turn a list of strings into an HTML list:

  <body>
<h1>JSX List</h1>
<div id="app"></div>
<script type="text/babel">
const allNames = ['McNulty', 'Jennings', 'Snyder',
'Meltzer', 'Bilas', 'Lichterman']
ReactDOM.render(
<ul>{allNames.map((name) => <li>{name}</li> )}</ul>,
document.getElementById('app')
)
</script>
</body>


We have to use map rather than a loop because whatever code we run has to return something that can be inserted into the DOM, and for loops don’t return anything. (We could use a loop to build up a string through repeated concatenation, but map is cleaner.) And note: we must return exactly one node, because this is one function call. We will look in the exercises at why the curly braces immediately inside the <ul> element are necessary.

Note also that when we run this, the browser console will warn us that each list element ought to have a unique key property, because React wants each element of the page to be selectable. We will add this later.

## Creating Components

One of the most powerful features of React is that it lets us create new components that look like custom HTML tags, but are associated with functions that we write. React requires the names of these components to start with a capital letter to differentiate them from regular tags. We can, for example, define a function ListOfNames to generate our list of names, then put that element directly in ReactDOM.Render just as we would put an h1 or p:

  <body>
<h1>Create Component</h1>
<div id="app"></div>
<script type="text/babel">
const allNames = ['McNulty', 'Jennings', 'Snyder',
'Meltzer', 'Bilas', 'Lichterman']

const ListOfNames = () => {
return (<ul>{allNames.map((name) => <li>{name}</li> )}</ul>)
}

ReactDOM.render(
<ListOfNames />,
document.getElementById('app')
)
</script>
</body>


What we really want to do, though, is pass parameters to these components: after all, JSX is turning them into functions, and functions are far more useful when we can give them data. In React, all the attributes we put inside the component’s tag are passed to our function in a single props object:

  <body>
<h1>Pass Parameters</h1>
<div id="app"></div>
<script type="text/babel">
const allNames = ['McNulty', 'Jennings', 'Snyder',
'Meltzer', 'Bilas', 'Lichterman']

const ListElement = (props) =>
(<li id={props.name}><em>{props.name}</em></li>)

ReactDOM.render(
<ul>{allNames.map((name) => <ListElement name={name} /> )}</ul>,
document.getElementById('app')
)
</script>
</body>


If you look carefully, you’ll see that the name attribute passed to the use of ListElement becomes the value of prop.names inside the function that implements ListElement. This gives us exactly one logical place to do calculations, set style, etc.

## Developing with Parcel

Putting all of the source for an application in one HTML file is a bad practice, but we’ve already seen the race conditions that can arise when we load JavaScript in a page’s header. And what about require statements? The browser will try to load the required files when those statements run, but who is going to serve them?

The solution is use a bundler to combine everything into one big file, and to run a local server to preview our application during development. However, this solution brings with it another problem: which bundler to choose? As with front-end frameworks, there are many to choose from, and new ones are being added almost weekly. Webpack is probably the most widely used, but it is rather complex, so we will use Parcel, which is younger and therefore not yet bloated (but give it time).

To install Parcel, run:

$npm install parcel-bundler  Once it’s in place, we can tell it to run one of our test pages like this: $ node_modules/.bin/parcel serve -p 4000 src/dynamic/pass-params.html

Server running at http://localhost:4000
+ Built in 128ms.


This works because when NPM installs a library in a project’s node_modules directory, it will sometimes put a runnable script associated with that library in node_modules/.bin (note that it’s .bin with a leading ., not bin). When we ask Parcel to serve up an application, it:

• looks in the named file to find JavaScript,
• looks recursively at what that file loads,
• copies some files into a directory called ./dist (which stands for “distribution”), and
• serves the application out of there.

Parcel also caches things in ./.cache so that it doesn’t need to do redundant work; both directories are normally added to .gitignore. To learn more about Parcel, see Sebastian Eschweiler’s quick tutorial.

It’s very common to put tasks like “run my application” into NPM’s package.json file, just as older programmers would put frequently-used commands into a project’s Makefile. Look for the section in package.json whose key is "scripts" and add this:

  "scripts": {
"dev": "parcel serve -p 4000",
...
},


We can now use npm run dev -- src/dynamic/pass-params.html, since everything after -- is passed to the script being run. This doesn’t just save us typing; it also gives other developers a record of how to use the project. Unfortunately, there is no standard way to add comments to a JSON file…

Whoops

Note: if we accidentally specify the name of a directory like src/dynamic instead of the name of an HTML file, Parcel prints an error on the console saying “no entries found”. This happens because it is trying to read the actual directory structure as if it were a file.

## Multiple Files

Now that we can bundle things up, let’s move our JSX into app.js and load that in the head of the page:

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>Hello Separate</title>
<script src="https://fb.me/react-15.0.1.js"></script>
<script src="https://fb.me/react-dom-15.0.1.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.js"></script>
<script src="app.js"></script>
<body>
<h1>Hello Separate</h1>
<div id="app"></div>
</body>
</html>


For now, the JavaScript in app.js is:

ReactDOM.render(
<p>Rendered by React</p>,
document.getElementById("app")
)


When we load this page we get the h1 title but not the paragraph. When we look in the browser console, we see the message:

Error: _registerComponent(...): Target container is not a DOM element.


This is the same race condition that has bitten us before. After sighing in frustration and making ourselves another cup of tea, we decide that, to keep things simple, we will load the script in the body of the page:

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>Hello Bottom</title>
<body>
<h1>Hello Bottom</h1>
<div id="app"></div>
<script src="./app.js"></script>
</body>
</html>


More importantly, we will rewrite app.js so that it loads the libraries it needs, because there’s no guarantee that libraries loaded in head will be available when app.js runs:

const React = require('react')
const ReactDOM = require('react-dom')

ReactDOM.render(
<p>Rendered by React</p>,
document.getElementById('app')
)


We don’t have to shut down the server and restart it every time we make changes like this, because Parcel watches for changes in files and relaunches itself as needed. Each time it does so, it looks at the libraries app.js loads and rebundles what it needs: right now, for example, dist/app.ef6b320b.js is 19930 lines long.

A more modern option than loading in the bottom is to add the async attribute to the script in the head of the page, which tells the browser not to do anything with the JavaScript until the page has finished building:

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>Hello Parcel</title>
<script src="./app.js" async></script>
<body>
<div id="app"></div>
</body>
</html>


## Exercises

### Those Damn Curly Braces

Our list-building example includes this line of code:

        <ul>{allNames.map((name) => <li>{name}</li> )}</ul>,


Why are the curly braces immediately inside the <ul> element necessary? What happens if you take them out?

### Real Data

1. Create a file called programmers.js that defines a list of JSON objects called programmers with firstName and lastName fields for our programmers. (You can search the Internet to find their names.)
2. Load that file in your page like any other JavaScript file.
3. Delete the list allNames from the application and modify it to use data from the list programmers instead.

### Ordering

What happens if you change the order in which the JavaScript files are loaded in your web page? For example, what happens if you load app.js before you load ListElement.js?

### Multiple Targets

What happens if your HTML page contains two div elements with id="app"?

### Creating a Component for Names

Create a new React component that renders a name, and modify the example to use it instead of always displaying names in <li> elements.

### Striping

Suppose we want to render every second list element in italics. (This would be a horrible design, but once we start creating tables, we might want to highlight alternate rows in different background colors to make it easier to read.) Modify the application so that even-numbered list elements are <li>{name}</li> and odd-numbered list elements are <li><em>{name}</em></li>. (You may want to use the fact that a map callback can have two parameters instead of one.)

## Key Points

• Older dynamic web sites generated pages on the server.
• Newer dynamic web sites generate pages in the client.
• React is a JavaScript library for client-side page generation that represents HTML elements as function calls.
• React replaces page elements with dynamically-generated content in memory (not on disk).
• React functions can be customized with elements.
• JSX translates HTML into React function calls so that HTML and JavaScript can be mixed freely.
• Use Babel to translate JSX into JavaScript in the browser.
• Define new React components with a pseudo-HTML element and a corresponding function.
• Attributes to pseudo-HTML are passed to the JavaScript function as a props object.