In this part of the guide we will setup the basic project structure, with an empty HTML document and configure babel to compile our JSX templates.
We need to install nodejs and babel so we can compile our JSX (React) code. If you have already installed nodejs, you can skip this section!
For Mac users:
- Go to https://nodejs.org/en/ and download & run the installer
- Open the Terminal (found in /Applications/Utilities)
- Run
sudo npm install --global babel
- Type admin password when prompted.
For windows users:
- Go to https://nodejs.org/en/ and download & run the installer
- Open the Command Prompt
- Run
npm install --global babel
To check it all worked fine, for both Windows or Mac, you can type babel --version
in the Terminal or Command Prompt. If you see something along the lines of "5.8.23 (babel-core 5.8.25)", it worked.
It is common practice for JavaScript applications to put their code in a folder called src
, which will then get compiled into another folder called build
. We'll use a program called Babel to do this compiling for us, which means, once set up, we won't need to worry about the build folder at all.
Create a new folder called todo-app
to contain the project. Within this folder, make a folder called vendor
and another one called src
. We'll put external JavaScript frameworks in vendor
, and we'll write our own code in src
. This keeps things nice and separated.
Next, we'll add the React framework file. You can find the framework on the USB drive in /files/react.js
. Copy it into your project's vendor
folder.
Next, let's setup our blank HTML document. Create a file called index.html directly in the todo-app
folder. This is the file which will load all the JavaScript files we write, and will be where the app all comes together. Write the following in index.html
to load the react.js
file we copied over and set up an empty <div>
element which we can later load our application into:
<!DOCTYPE html>
<html>
<head>
<title>Todo App</title>
<meta charset="utf-8">
</head>
<body>
<div id="app"></div>
<script src="vendor/react.js"></script>
</body>
</html>
Now we have the basic structure setup, let's write our first bit of JavaScript. Before we go and start using React, lets make sure we can run the app with Babel and have our JavaScript compile.
In the src
folder, create a file called app.js
. This will be the entry point of our application later, but for now, let's just write something which we can immedediately see works when we load the page. In JavaScript, you can trigger a popup on the page using the alert()
function, so lets just use that to say "Hello world!". In app.js
, add the following:
alert("Hello world!");
Finally, let's include this new file in our index.html
document to have it run when we load the page. Below the <script>
tag where we import React, add:
<script src="build/app.js"></script>
Notice we're including the file from the build folder instead of src. Currently that file doesn't exist, but once we start compiling our JavaScript, it'll be created for us by Babel.
Now we have everything setup, lets build our JavaScript and open our app in Chrome. To build the JavaScript, in your Terminal (or Command Prompt for Windows users), run the following:
babel src --watch --out-dir build
This tells Babel to watch for any changes made within src
and automatically compile them into build
. You can leave this running while you continue with the rest of the tutorial.
Now we can open our app in Chrome! Switch to Chrome and choose File > Open. Navigate to your index.html
file. When it loads, you should see the "Hello world!" alert popup!
We're not going to cover CSS in this tutorial, if you're familiar with CSS feel free to style the todo list as you'd like! Or, if you'd like to hit the ground running, we've included a premade stylesheet on the USB stick under files/style.css
. If you'd like to use it, copy it into the base project folder, and then update the <head>
of index.html
to link to the stylesheet, like so:
<head>
<title>Todo App</title>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css">
</head>