Table of Contents
In this lesson we will setup our project and build our first React application.
- Languages and Editor - select the working language and the editor.
- Setup - install the appropriate packages to setup the application.
- First application - run your first application.
- Libraries - add third party libraries for the application.
- Deploy application - publish the application.
The official React documents can be found in React and Create React App.
Language and Editor
Several JavaScript compiled languages can be used with React:
- ECMAScript (ES5) - defines JavaScript old standard.
- ES2015 (ES6) and above - are the advanced JavaScript standards. Must be transpiled to the old JavaScript (ES5) if not supported by some browsers. ES6 defines many new features as classes, let, arrow function, ...
- TypeScript - TypeScript is superset of ES6, which is superset of ES5. In addition to ES6 features, TypeScript adds strong typing, syntax checking ... Thus, it produces less runtime errors because of compile-time type checking. TypeScript must be transpiled to JavaScript before using by browsers.
JavaScript with ES6 features will be used in this lecture. We will use Visual Studio Code editor.
Installation
There are a few different ways to get started with an React project. You can also add React component to existing HTML document. You can see how in the official React documentation here.
We will use the environment that Facebook has created which is pre-configured with everything you need to build a React application. It will create a live development server, use Webpack to automatically compile React, JSX, and ES6, auto-prefix CSS files, and use ESLint to test and warn about mistakes in the code.
Before we can begin, you need to ensure you have installed:
- Node.js with NPM (Node Package Manager) - npm is the package manager for JavaScript. It allows to find and download libraries (or packages) and update the libraries by typing a command in the command line. It hosts almost half a million packages of free, reusable code — the largest software registry in the world. Use npm to install code into your project.
To check whether or not you have Node.js installed, visit your console / command line and type:
node -v
We need a Node version >= 8.10 and npm >= 5.6
If this command goes unrecognized or it is improper version, you need to install Node.js. Go to Node.js, choose either the Windows or Mac installer based on your OS, and download it. By default, it will install the npm package manager which we will need.
After it's installed, close your console / command line and reload it. You can now run the node -v command and it will provide you with the current version number. - React Developer Tools fro Chrome - Chrome extension
Now create React application (Create React App and here ).
It is done by executing in the terminal:
npx create-react-app my-app
my-app is the name you give to your application (you can use any name). The command creates the folder my-app.
Create React App is a comfortable environment for learning React, and is the best way to start building a new single-page application in React.
It sets up your development environment so that you can use the latest JavaScript features, provides a nice developer experience, and optimizes your app for production.
Go to my-app and open live server by:
cd my-app npm start
Read about folder structure in Folder Structure.
First Application
Generate React project as described above. Write in index.js the following and run npm start.
/***************************************** * * IMPORT LIBRARIES *****************************************/ import React from 'react'; import ReactDOM from 'react-dom'; // import our css import './index.css'; /***************************************** * * SHOW CODE *****************************************/ ReactDOM.render(<h1>Hello Class!</h1>, document.getElementById('root')); /* JSX of the above is translated to JS like: var h1 = document.createElement('h1'); h1.innerHTML = 'Hello Class!'; document.getElementById('root').appendChild(h1); */
Libraries
Sometimes we need to load external global scripts or CSS files into our projects (for example Bootstrap, jQuery, ...).
A quick and easy method of integrating frameworks like Bootstrap or jQuery is to simply link to a hosted version of the framework (by CDN link) within the <head> tags of the index.html file located in the app folder. You can use the npm to install node packages that contain the files to these frameworks and then to use import statement.
You can find how to install libraries in Installing a Dependency folder. Here you can see Bootstrap and other examples.
Bootstrap
To include Bootstrap write the following command at the console ad the application root folder:
npm install bootstrap --save
The --save option instructed NPM to include the package inside of the dependencies section of your package.json automatically.
Import Bootstrap CSS and optionally Bootstrap theme CSS in the beginning of your src/index.js file:
import 'bootstrap/dist/css/bootstrap.css';
SASS
To include SASS write the following command at the console ad the application root folder:
npm install node-sass
SASS files file extension is .scss
Normalize.css
If you don't include bootstrap, add normalize.css to your application by writing in index.css:
@import-normalize;
normalize.css makes browsers render all elements more consistently and in line with modern standards. It precisely targets only the styles that need normalizing. Since bootstrap library makes use of normalize.css, it is not required while using bootstrap.
See Adding a CSS Reset folder
Deploy Application
First remove libraries that you don't use, especially unnecessary scripts in your modules definition. Consider smaller alternatives to the libraries that you do use.
Define your site path in package.json like:
"homepage": "http://bridge-tech-edu.com"
Run the following command in the console within an React project root folder:
npm run build
This will create a 'build' folder with a production build of your app. For more information see Deployment.