The next thing we need to do is initiate our application as an npm
project. Let's try it out, and then we'll discuss why we needed to do so:
- Inside your
project
folder, in your terminal, typeyarn init
and hit enter. - It’ll ask you a series of questions. The first one is the most important--the name of our application. It should just take the name of the current folder (
chatastrophe
). If it doesn't, just enterchatastrophe
. From there, just hit enter to skip the rest of the questions, accepting the default answers. These questions would matter more if we were planning on publishing our own package, but we're not, so no worries! - If you take a look at your project folder after completing the yarn init, you'll notice that it added a
package.json
file with our project name and version. Ourpackage.json
is important, in that it will act as a list of our dependencies--the package we will install viayarn
.
Enough talking about dependencies, though, let's install our first one! What better choice than to install React?
Let’s try it by running yarn add [email protected]
from within your project
folder.
Note
We're installing a specific version of React (15.6.1) to ensure compatibility with other dependencies, and to ensure that there are no unexpected problems as new versions are released.
Once the installation is complete, you should see React added to our package.json
under dependencies. You'll also see that yarn
generated a node_modules
folder and a yarn.lock
file.
The node_modules
folder is where all our packages will live. If you open it up, you can see that there are several folders already. We've not only installed React, but everything that React depends on--dependencies on dependencies.
As you might imagine, the node_modules
folder can get quite hefty. So, we don't check it into source control. When a new developer joins the team and downloads the project files, they can then install the dependencies independently, based on the package.json
; this saves time and space.
However, we need to ensure that they get the same packages as everyone else, and the same version; this is where the yarn.lock
file comes in.
The previously mentioned setup ensures that we are ready to safely use third-party libraries. We have the package.json
, yarn.lock
, and node_modules
folders in our project. Before we continue, let's ensure that adding React worked.
Let's confirm that React is in our project by using it to render a simple element to our screen. This will be our first dipping of our feet into React, so go slow and ensure that you understand each step.
First, we need to import our React package (which we just installed with yarn
) into our index.html
so that we can use it there.
To do this, we add a <script>
tag with the path to the main React file within our node-modules
folder. This tag looks like this:
<script src="../node_modules/react/dist/react.js"></script>
Place this in your index.html
, at the bottom of the body
tag (before the closing </body>
).
Okay, we have React! Let's use it to make a simple <h1>
tag, just like the one we wrote in HTML.
React has a function called createElement
for this purpose. It takes three arguments: element type, something called props (more on that later), and the children (what goes inside the tag).
For us, it looks like this:
React.createElement('h1', null, 'Hello from React!')
This function call creates an element that looks as follows:
<h1>Hello from React!</h1>
To confirm that it will work, let's console.log
it out:
<script src="../node_modules/react/dist/react.js"></script> <script> console.log(React.createElement('h1', null, 'Hello from react!')) </script>
Reload index.html
, then right-click or control-click and select Inspect
to open up DevTools in Chrome and switch to the Console
tab. There, we see our element… or not. Instead of the HTML output, we get something like this:
This is not the HTML element we might have expected, but we can see that React has worked in its own way. We have a JavaScript object with a type field of h1
. Let’s see whether we can transform this into an actual HTML tag on the screen.