Introducing Single Page Applications
React can be used to simplify the creation of complex user interfaces, and there are two main ways of doing that:
- Manage parts of a website (e.g., a chat box in the bottom left corner).
- Manage the entire page and all user interaction that occurs on it.
Both approaches are viable, but the more popular and common scenario is the second one: Using React to manage the entire web page, instead of just parts of it. This approach is more popular because most websites that have complex user interfaces, have not just one complex element but multiple elements on their pages. Complexity would actually increase if you were to start using React for some website parts without using it for other areas of the site. For this reason, it's very common to manage the entire website with React.
This doesn't even stop after using React on one specific page of the site. Indeed, React can be used to handle URL path changes and update the parts of the page that need to be updated in order to reflect the new page that should be loaded. This functionality is called routing and third-party packages like react-router-dom
(see Chapter 12, Multipage Apps with React Router), which integrate with React, allow you to create a website wherein the entire user interfaces is controlled via React.
A website that does not just use React for parts of its pages but instead for all subpages and for routing is called a Single Page Application (
SPA)
because it consists of only one HTML file (typically named index.html
) which is used to initially load the React JavaScript code. Thereafter, the React library and your React code take over and control the actual user interface. This means that the entire user interface is created and managed by JavaScript via React and your React code.
Creating a React Project
To work with React, the first step is the creation of a React project. This can be done in multiple ways, but the most straightforward and easiest way is to use the create-react-app
utility command line tool. This is a tool maintained by (parts of) the React team, and you can install it as a Node.js package via the Node Package Manager (npm). Once installed, this tool can be used to create a project folder that comes with React pre-installed, as well as some other tools, such as the Jest package for automated testing.
You need a project setup like this because you typically use features like JSX which wouldn't work in the browser without prior code transformation. Therefore, as mentioned earlier, a pre-processing step is required, and the React project created via create-react-app
includes such a step as part of the code build workflow.
To create a project with create-react-app
, you must have Node.js installed—preferably the latest (or latest LTS) version. You can get the official Node.js installer for all operating systems from https://nodejs.org/. Once you have installed Node.js, you will also gain access to the built-in npm
and npx
commands, which you can use to utilize the create-react-app
package to create a new project.
You can run the following command inside of your command prompt (Windows), bash (Linux), or terminal (macOS) program. Just make sure that you navigated (via cd
) into the folder in which you want to create your new project.
npx create-react-app my-react-project
This command will create a new subfolder with a basic React project setup (i.e., with various files and folders) in the place where you ran it. You should run it in some path on your system where you have full read and write access and where you're not conflicting with any system or other project files.
The exact project structure (that is, the file names and folder names) may vary over time, but generally, every new React project contains a couple of key files and folders:
- A
src/
folder that contains the main source code files for the project:- An
index.js
file which is the main entry script file that will be executed first - An
App.js
file which contains the root component of the application (you'll learn more about components in the next chapter) - Various styling (
*.css
) files that are imported by the JavaScript files - Other files, like code files for automated tests
- An
- A
public/
folder which contains static files that will be part of the final website:- This folder may contain static images like favicons
- The folder also contains an
index.html
file which is the single HTML page of this website
package.json
andpackage-lock.json
are files that manage third-party dependencies of your project:- Production dependencies like
react
orreact-dom
- Development dependencies like
jest
for automated tests
Note
package.json
is the file in which you actually manage packages and their versions.package-lock.json
is created automatically (byNode.js
). It locks in exact dependency and sub-dependency versions, whereaspackage.json
only specifies version ranges. You can learn more about these files and package versions on https://docs.npmjs.com/.- Production dependencies like
- The
node_modules
folder holds the actual third-party package code of the packages that are listed in thepackage.json
file. Thisnode_modules
folder can be deleted since you can recreate it by runningnpm install
inside of the project folder
Most of the React code will be written in the App.js
file or custom components that will be added to the project. This book will explore components in the next chapter.
Note
The node_modules
folder can become very big since it contains all projects dependencies and dependencies of dependencies. Therefore, it's typically not included if projects are shared with other developers or pushed to GitHub. The package.json
file is all you need. By running npm install
, the node_modules
folder will be recreated locally.
Once the project is created, you can start writing your code. To preview your code on a live website locally on your system, you can run npm start
inside of the project folder. This will start a built-in development server that pre-processes, builds, and hosts your React-powered SPA. This process should normally open the preview page in a new browser tab automatically. If that doesn't happen, you can manually open a new tab and navigate to localhost:3000
there (unless you see a different address as output in the window where you executed npm start
, in which case, use the address that's shown after you ran npm start
).
The preview website that opens up will automatically reload and reflect code changes whenever you save changes to your code.
When you're done with development for the day, you can quit the running development server process via CTRL + C
(in the command prompt or terminal window where you started it via npm start
). To continue development and get back that live preview website, you can always restart it by running npm start
(inside of the project folder) again.